I am just starting to learn the OO side of Python scripting, and I am
a little confused on the following. Take the following example class:
[color=blue][color=green][color=darkred]
>>> class rectangle(objec t):[/color][/color][/color]
z = 1
def __init__(self):
self.x = 2
[color=blue][color=green][color=darkred]
>>> r = rectangle()
>>> print r.z[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> print r.x[/color][/color][/color]
2[color=blue][color=green][color=darkred]
>>> r.z = 16
>>> print r.z[/color][/color][/color]
16[color=blue][color=green][color=darkred]
>>> r.x = 17
>>> print r.x[/color][/color][/color]
17[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)
thanks
-- brian
a little confused on the following. Take the following example class:
[color=blue][color=green][color=darkred]
>>> class rectangle(objec t):[/color][/color][/color]
z = 1
def __init__(self):
self.x = 2
[color=blue][color=green][color=darkred]
>>> r = rectangle()
>>> print r.z[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> print r.x[/color][/color][/color]
2[color=blue][color=green][color=darkred]
>>> r.z = 16
>>> print r.z[/color][/color][/color]
16[color=blue][color=green][color=darkred]
>>> r.x = 17
>>> print r.x[/color][/color][/color]
17[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)
thanks
-- brian
Comment