Andreas Kostyrka escribió: > Because your atribute is a class attribute: > > class C: > ca = 123 > > print C.ca # 123 > c1 = C() > print c1.ca # 123 > c1.ca = 140 > print c1.ca # 140 > print C.ca # 123 > c2 = C() > print c2.ca # 123 > C.ca = 141 > print C.ca # 141 > print c1.ca # 140 > print c2.ca # 141 > > Basically, when an instance does not have an attribute, it looks them up > in the class, which might recurse into base classes. > > Furthermore, objects & classes 101 material: > > class C: > def method(self): > print self > > c = C() > print c.method # bound method to c > print C.method # unbound method, checks that first argument is a C > print C.__dict__["method"] > # function, does NOT check first argument. > > So basically, the whole self argument handling is magic that happpens > during attribute lookup. > > Andreas > > Am Freitag, den 03.11.2006, 14:27 +0100 schrieb euoar: > >> I think I don't understand the OOP in python, could anyone explain why >> this code works? >> >> class example: >> atribute = "hello world" >> >> print example.atribute >> >> Why you don't have to make an object of the class to access to the >> atribute? >> >> ( class example: >> atribute = "hello world" >> >> obj = example() >> print obj.atribute >> >> >> Thanks in advance. >> >> >> ______________________________________________ >> LLama Gratis a cualquier PC del Mundo. >> Llamadas a fijos y móviles desde 1 céntimo por minuto. >> http://es.voice.yahoo.com >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> Thank you for your answer and the examples. So without self it is an instance variable (like "static" in java/c#). But then, I don't understand how is it possible this in your example:
c1.ca = 140 or down: print C.method Are you creating new atributes and methods at run time? Is that what has happened? In fact I have tried also this: class example: atribute = "green" obj = example() obj.a_new_atribute = "white" And even this seems to be correct: class example: atribute = "green" example._other_atribute = "yellow" So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? ______________________________________________ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor