2

I am curious how python class/instance method connects

and I make a class with three methods (instance/class/static)

class Asian_loser(object):
    def kerker(self):
        w=1
    @classmethod
    def loss(self):
        print 123
    @staticmethod
    def wwww():
        print 123

and see its' id

print(id(Asian_loser.loss))
print(id(Asian_loser.kerker))
print(id(Asian_loser.wwww))

the loss and kerker method id will be the same and vary for a few seconds later

and I make a instance from Asian_loser

w=Asian_loser()

print(id(w.kerker))
print(id(w.loss))
print(id(w.wwww))

the loss and kerker method id will be the same and vary for a few seconds later

just want to know what accutally happen

楊亮魯
  • 61
  • 7
  • The `id`s are not varying over time for me. – James Nov 15 '16 at 03:02
  • What Python version do you use? The implementation details for this stuff went through some big changes between Python 2 and 3. – wim Nov 15 '16 at 03:03
  • I'm using python 2.7 – 楊亮魯 Nov 15 '16 at 03:04
  • It seems that Bound & unbound methods are almost temporary instance objects. will it get different result using python 3? – 楊亮魯 Nov 15 '16 at 03:05
  • Probably. The concept of a bound method doesn't really exist in Python 3. In Python 2 methods are actually [descriptor objects](https://docs.python.org/2/reference/datamodel.html#invoking-descriptors). – wim Nov 15 '16 at 03:08
  • [This](http://stackoverflow.com/q/11949808/674039) Q&A might help for you. – wim Nov 15 '16 at 03:20
  • Hi wim , thx for you post . it's really helpful , maybe you can answer this question and thus I can close this question – 楊亮魯 Nov 15 '16 at 05:30

0 Answers0