Re: Puzzled by behaviour of class with empty constructor

2008-01-25 Thread dbaston
On Jan 25, 5:46 pm, Bjoern Schliessmann  wrote:
> [EMAIL PROTECTED] wrote:
> > 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
>
> --
> BOFH excuse #49:
>
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Puzzled by behaviour of class with empty constructor

2008-01-25 Thread 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:
>>> [(0, 2)] [(0, 2)] [(0, 2)]

Can anyone explain this?
-- 
http://mail.python.org/mailman/listinfo/python-list