Why Are References To Instance Methods Stored In Each Instance Object Rather Than In The Class Object?
Solution 1:
Attribute lookup on objects in Python is non-trivial. But instance methods are certainly not stored on the instance object!
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance,
a.x
has a lookup chain starting witha.__dict__['x']
, thentype(a).__dict__['x']
, and continuing through the base classes oftype(a)
excluding metaclasses.
(docs)
Note that it is possible to store a function on an instance. But that's not an instance method! When the interpreter looks up an attribute and finds that it is (a) a function and (b) on the class object, it automatically wraps it in a bound method object which passes self
.
Is there any way to look at (and perhaps change) the names and the references to instance methods?
Well, you can certainly modify the class object after defining it. But I assume what you mean is "can you make the x
method of a particular instance do something different?"
This being Python, the answer is "yes": just define a.x
to be some new function. Then you will get that function back before looking on the class.
This may cause you a lot of confusion when you're trying to understand the code, though!
Solution 2:
From what I understand, each instance of a class stores references to the instance's methods.
I don't know where you got this from, but it's wrong. They don't.
Why are instance methods not accessible in a way similar to instance attributes, i.e., through
__dict__
, or through some other system attribute?
Well, because they are not stored on the instance.
Is there any way to look at (and perhaps change) the names and the references to instance methods?
Since these references don't exist, you cannot change them. You can of course create any attribute you want by normal assignments, but note that functions stored on the instance are not treated like ordinary methods -- the mechanism that implicitly passes the self
parameter does not apply for them.
Solution 3:
Incorrect. Instances do not store references to each method.
For example:
classFoo():
defbar(self):
print'bar'
f = Foo()
defalternate_bar(self):
print'alternate bar'
f.bar()
Foo.bar = alternate_bar # modifies the class!
f.bar()
prints
bar
alternate bar
This is also why you provide a self
to each method you define in a class. Without a reference to self
, the method has no idea which instance it is working on.
Solution 4:
Another example
classPoint:
def__init__(self, xcoord, ycoord):
self.x = xcoord
self.y = ycoord
defdraw(self):
print self.x, " ", self.y
p = Point(205.12, 305.21)
#draw the coordinates of the point instance
p.draw()
# now define a new point drawing function vdraw()defvdraw(q):
print"[",q.x,",",q.y,"]"#p.draw()#now reassign the draw() method to vdraw()
Point.draw = vdraw
# now print the coordinates of the point instance print p.x
print p.y
#now draw the coordinates of the point instance
p.draw()
Post a Comment for "Why Are References To Instance Methods Stored In Each Instance Object Rather Than In The Class Object?"