-5

I've got a class Foo that's a running thread, what I'd like to do is limit how much class Bar can access of Foo while still having access to Foo's internals, is that possible?

Levi Campbell
  • 5,903
  • 7
  • 38
  • 47
  • 2
    Your question is unclear. Are you asking if it is possible to make a new instance of an object? – Crashworks Nov 06 '09 at 08:30
  • 1
    Alex Martelli's answer at http://stackoverflow.com/questions/1547145/defining-private-module-functions-in-python is a good answer to this question. – Greg Hewgill Nov 06 '09 at 09:10
  • 1
    Seeing the answers below, next time you should put your question clearly from the start and not change it so completely, it's most confusing and wasted time for the others who are trying to help. – RedGlyph Nov 06 '09 at 10:27
  • 1
    You should rename your question! – Casebash Nov 08 '09 at 00:50
  • I'm sorry I've been so busy with starting a new company I've just ended this question. – Levi Campbell Jan 02 '10 at 11:47

2 Answers2

2

Python is a strongly, dynamically typed language. What this means is:

  • Objects are strongly typed which means an integer is an integer and can't be treated as anything else unless you say so. Objects have a specific type and stay that way.
  • You can use a name (a variable) to refer to an object, but the name doesn't have any particular type. It all depends on what the name refers to, and this can change as other things are assigned to the same name.

Python strongly makes use of the so-called "duck typing" technique where objects do not have (and do not need) specifically typed interfaces. If an object supports a certain set of methods (the canonical example is a file-like object), then it can be used in a context that expects file-like objects.

Greg Hewgill
  • 890,778
  • 177
  • 1,125
  • 1,260
  • I had to see the original question to understand how it was related, sorry the OP changed it so dramatically... – RedGlyph Nov 06 '09 at 10:23
0

According to your question, it looks that you want an instance of IFoo that may act like Foo. Following code does that, but its not recommended to do it that way in Python.

class IFoo(object): pass
class Foo(IFoo): pass

f = IFoo()
Foo.__init__(f)

Better way is to simply use (multi)inheritance:

class IFoo(object):
    def __init__(self, *args, **kwargs):
        pass

class Foo(IFoo):
    def __init__(self, *args, **kwargs):
        IFoo.__init__(self, *args, **kwargs)

f = Foo()
mtasic85
  • 3,684
  • 2
  • 18
  • 28