[Tutor] question about classes and atributes

Andreas Kostyrka andreas at kostyrka.org
Fri Nov 3 15:17:48 CET 2006


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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20061103/09d14e7c/attachment.pgp 


More information about the Tutor mailing list