[Tutor] question about classes and atributes

Luke Paireepinart rabidpoobear at gmail.com
Fri Nov 3 17:07:07 CET 2006


>>> 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?
>>>       
because that attribute is part of the Class object that's created when 
you declare a class.

As you can see by the following code:
 >>> class example:
    attribute = 'hello, world!'
 >>> example
<class __main__.example at 0x00B528D0>

there is actually a type of object called 'class.'
when you make an instance of a class,
 >>> a = example()
 >>> a
<__main__.example instance at 0x00B40440>

it is now a example object instance.
> 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 
>   
Because c1 is an instance of the class 'C', it has the attribute .ca 
already in it.
This is a reference to the class attribute 'ca' which is equal to 123.
However, when you change c1.ca, because the class attribute 'ca' is 
immutable (since it's an integer)
a local copy of ca is created with the value 140 in it.
C.ca is still 123.
If you now do C.ca = 234,
c1.ca is still 140, because it now has a local (instance) attribute 
called 'ca' that hides the class attribute.
However, if you do something like this...
class C:
    ca = 123
c1 = C()
C.ca = 567
print c1.ca

you will get an output of '567'.
because c1 never got the local instance attribute to replace the 
reference to the class-wide attribute,
so changing the C.ca will also affect c1.ca.

> 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?
>   
No, you're confusing Python with a compiled language.
You're not adding methods to an object at run-time because there's not a 
distinction between runtime and compile-time in Python,
because compile-time doesn't exist.
You can add attributes to classes any time you want inside your program.
Just like I can add an element to a list any time I want.
a = [1,2,3]
a.append(4)
Classes are just objects, as lists are, and integers are, and .... 
everything else is as well.
And when I execute the code, Python knows how to do all of these things.

You see, an interpreted session is not the same as you think of 'at run 
time' being.
For the most part, an interpreted session is exactly the same as if I 
were to type the code into a text document,
save it, and execute it.
So yeah, anywhere in your program you can add methods to classes,
but really saying 'at run-time' is confusing terminology.
It implies that if I were running someone else's program I could just
add methods in on the fly whenever I wanted to.
This is not true, unless they've enabled this functionality.


HTH,
-Luke


More information about the Tutor mailing list