Puzzled by behaviour of class with empty constructor

D

dbaston

Hello,

I have a class called 'Axis' that I use as a base class for several
types of axes that can be created by a grid generation program that I
have written: equally-spaced grids, logarithmic grids, etc. In any
case, if I use this base class by itself, I see some puzzling
behaviour:
#############
class Axis:
ends = []
N = None
def __init__(self):
pass

x = Axis()
y = Axis()
z = Axis()

x.ends.append((0,2))

print x.ends,y.ends,z.ends
#############
Running the following code outputs:

Can anyone explain this?
 
B

Bjoern Schliessmann

print x.ends,y.ends,z.ends
#############
Running the following code outputs:
[(0, 2)] [(0, 2)] [(0, 2)]

Can anyone explain this?

Yes. You bound a single list to the name "ends" inside the class.
This name is shared by all instances.

If you want the instances to each have separate lists, delete
the "ends" definition from class declaration and insert "self.ends
= []" into __init__.

I also suggest you to have a look at the tutorial.

Regards,


Björn
 
T

Tomek Paczkowski

Hello,

I have a class called 'Axis' that I use as a base class for several
types of axes that can be created by a grid generation program that I
have written: equally-spaced grids, logarithmic grids, etc. In any
case, if I use this base class by itself, I see some puzzling
behaviour:
#############
class Axis:
ends = []
N = None
def __init__(self):
pass

x = Axis()
y = Axis()
z = Axis()

x.ends.append((0,2))

print x.ends,y.ends,z.ends
#############
Running the following code outputs:
[(0, 2)] [(0, 2)] [(0, 2)]

Can anyone explain this?

Well, you are using a class variable - Axis.ends. It's shared among all
instances of Axis class. To have it separate setup it in __init__ like:

class Axix:
def __init__(self):
self.ends = []
self.N = None

You see, code inside class, but outside methods is executed only once and
any variables are then linked with class, and not instances (more/less).
Take look at: http://docs.python.org/tut/node11.html

~TomekP
 
D

Diez B. Roggisch

Hello,

I have a class called 'Axis' that I use as a base class for several
types of axes that can be created by a grid generation program that I
have written: equally-spaced grids, logarithmic grids, etc. In any
case, if I use this base class by itself, I see some puzzling
behaviour:
#############
class Axis:
ends = []
N = None
def __init__(self):
pass

x = Axis()
y = Axis()
z = Axis()

x.ends.append((0,2))

print x.ends,y.ends,z.ends
#############
Running the following code outputs:
[(0, 2)] [(0, 2)] [(0, 2)]

Can anyone explain this?

It's simple - you didn't create an instance-variable, but instead a
class-variable.

You need to do this:

class Axis:

def __init__(self):
self.ends = []


Diez
 
D

dbaston

print x.ends,y.ends,z.ends
#############
Running the following code outputs:
[(0, 2)] [(0, 2)] [(0, 2)]
Can anyone explain this?

Yes. You bound a single list to the name "ends" inside the class.
This name is shared by all instances.

If you want the instances to each have separate lists, delete
the "ends" definition from class declaration and insert "self.ends
= []" into __init__.

I also suggest you to have a look at the tutorial.

Regards,

Björn

Björn,

Thanks for the help. I had misguidedly defined the members of all of
my classes as in the example above; I never noticed the issue with any
of the others because they did not have empty constructors.

Thanks again for the correction.
 
T

Tim Rau

print x.ends,y.ends,z.ends
#############
Running the following code outputs:
[(0, 2)] [(0, 2)] [(0, 2)]
Can anyone explain this?
Yes. You bound a single list to the name "ends" inside the class.
This name is shared by all instances.
If you want the instances to each have separate lists, delete
the "ends" definition from class declaration and insert "self.ends
= []" into __init__.
I also suggest you to have a look at the tutorial.
Bogon emissions

Björn,

Thanks for the help. I had misguidedly defined the members of all of
my classes as in the example above; I never noticed the issue with any
of the others because they did not have empty constructors.

Thanks again for the correction.

Yeah! thanks all. I did not realize the distinction either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top