[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Puzzled by behaviour of class with empty constructor

dbaston

1/25/2008 10:33:00 PM

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?
9 Answers

Bjoern Schliessmann

1/25/2008 10:46:00 PM

0

dbaston@gmail.com 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

Tomek Paczkowski

1/25/2008 10:52:00 PM

0

dbaston@gmail.com wrote:

> 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/n...

~TomekP

--
Someone whom you reject today, will reject you tomorrow.

Diez B. Roggisch

1/25/2008 10:54:00 PM

0

dbaston@gmail.com schrieb:
> 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

dbaston

1/25/2008 10:55:00 PM

0

On Jan 25, 5:46 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.com> wrote:
> dbas...@gmail.com 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.

NIm

1/26/2008 5:34:00 AM

0

On Jan 25, 5:54 pm, dbas...@gmail.com wrote:
> On Jan 25, 5:46 pm, Bjoern Schliessmann <usenet-
>
>
>
> mail-0306.20.chr0n...@spamgourmet.com> wrote:
> > dbas...@gmail.com 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.

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

Richard Steel

1/13/2012 8:12:00 PM

0


> Richard, of course, doesn't realise it, but the etymology of 'fuck' is
> quite interesting.

Barry doesn't realize it, of course....but he's an arrogance, insecure
fool who actually believes that people who disagree with him are all
illiterate bumpkins.

I cut the rest of what he wrote because I learned all about the
history of the work "fuck" back when I was ten years old -- the time
of life when it's the history of the word "fuck" is actually
interesting.

Richard Steel

1/14/2012 12:40:00 AM

0

On Jan 13, 3:02 pm, 2849 Dead <d...@gone.com> wrote:
> On Fri, 13 Jan 2012 12:12:28 -0800, Richard Steel wrote:
> >> Richard, of course, doesn't realise it, but the etymology of 'fuck' is
> >> quite interesting.
>
> > Barry doesn't realize it, of course....but he's an arrogance, insecure
> > fool who actually believes that people who disagree with him are all
> > illiterate bumpkins.
>
> > I cut the rest of what he wrote because I learned all about the history
> > of the work "fuck" back when I was ten years old -- the time of life
> > when it's the history of the word "fuck" is actually interesting.
>
> Don't blame Dr. Worthington for thinking you're an ignorant idiot; you
> put in so much time proving that you are over the years.

Worthington believes pretty much any America who isn't a socialist is
an idiot.


Richard Steel

1/14/2012 1:44:00 AM

0

On Jan 13, 5:32 pm, 2849 Dead <d...@gone.com> wrote:
> On Fri, 13 Jan 2012 16:39:38 -0800, Richard Steel wrote:
> > On Jan 13, 3:02 pm, 2849 Dead <d...@gone.com> wrote:
> >> On Fri, 13 Jan 2012 12:12:28 -0800, Richard Steel wrote:
> >> >> Richard, of course, doesn't realise it, but the etymology of 'fuck'
> >> >> is quite interesting.
>
> >> > Barry doesn't realize it, of course....but he's an arrogance,
> >> > insecure fool who actually believes that people who disagree with him
> >> > are all illiterate bumpkins.
>
> >> > I cut the rest of what he wrote because I learned all about the
> >> > history of the work "fuck" back when I was ten years old -- the time
> >> > of life when it's the history of the word "fuck" is actually
> >> > interesting.
>
> >> Don't blame Dr. Worthington for thinking you're an ignorant idiot; you
> >> put in so much time proving that you are over the years.
>
> > Worthington believes pretty much any America who isn't a socialist is an
> > idiot.
>
> He doesn't, but you exist to promote the 'idiot' theory.

http://www.thedailyshow.com/watch/thu-january-12-2012/civil-...

Matt

1/14/2012 1:55:00 AM

0

On Jan 12, 8:15 pm, Mr.B1ack <b...@barrk.net> wrote:
> Matt <matttel...@sprynet.com> wrote:
> >On Jan 11, 8:02 pm, Mr.B1ack <b...@barrk.net> wrote:
> >> Matt <matttel...@sprynet.com> wrote:
> >> >On Jan 11, 4:04 pm, Mr.B1ack <b...@barrk.net> wrote:
> >> >> Matt <matttel...@sprynet.com> wrote:
> >> >> >On Jan 11, 12:53 pm, Mr.B1ack <b...@barrk.net> wrote:
> >> >> >> Richard Steel <rsteel2...@aol.com> wrote:
> >> >Same has been done with Christianity, Hinduism, and every
> >> >other religion on the planet.
>
> >>    That's a statment of fact, not an excuse or
> >>    justification to anyone born in a Land Of
> >>    The Free.
>
> >>    In some 3rd-world shithole run by bomb-throwing
> >>    fanatics, maybe ......
>
> >Part of being in the Land of the Free is realizing that we
> >give free reins to those that wish to do really bad things.
> >The same arguments are often made for abortion or guns
> >or religion or free speech or...
>
>    Well, when you say "free", seems that most people
>    think it means "Free to do/think/say exactly what
>    *I* would do/think/say". The hardest part about
>    freedom is giving everyone else the slack to do
>    things THEIR way as much as practically possible.

When I say free, I mean it the way it was intended. I disagree with
you completely and will fight to the death for your right to be
disagreeable.

>
>    As Bob the Enlightened One would say "Gimme some slack !".

Yes, but he says that about the noose around his neck.

>
> >> >> Just because we're much more familiar with the
> >> >> garden-variety Jeezus nut in the USA doesn't mean
> >> >> they're not demanding anything any less poisonous
> >> >> to the whole Free Country theme than the Islamists
> >> >> are demanding of France or Denmark or whereever.
>
> >> >No argument from me.
>
> >>    Well that's no fun ....     :-)
>
> >I don't much bother to argue with the Jeezus nuts. What would be the
> >point? They are already convinced I'm evil.
>
>    A hermetically-sealed mental universe there ... long
>    ago I decided there was no point in arguing with them.
>    Pick a subject and produce a mountain of evidence and
>    a mile of sound reasoning ... and all THEY have to do
>    to 'win' the debate is say "I *believe*" and that's that.
>    If they run out of facts they skip to legends. If they
>    run out of reason they evoke magic. If 2+2 needs to
>    equal 5 to fill in a gap in their theology then, well,
>    their gawd will just MIRACLE it into being 5.

Hm. Tim Tebowism?

>
>    MINOR problem ... they're allowed to VOTE and serve
>    on JURIES and hold high OFFICES - so their universe
>    forever tries to rewrite everyone elses. They have
>    the *right* to try, it's part of their freedoms, but
>    that doesn't necessarily mean it's a GOOD idea.

I know. The problem is, they ARE allowed to vote, and serve
on juries and hold office and even spout about their insanity
in the news or books on on TV. And sadly, I respect that right.
See previous.

>
>    Of course 'democracy' isn't ABOUT finding the *right*
>    answers ... only the most *popular* ones. As the man
>    said, the worst system of government imaginible, except
>    for all the others ....   :-)

There are many reasons we are not a democracy. The problem
is, our founding fathers never really anticipated that those
representing
us in the Republic would be idiots.

> >> >> >But they
> >> >> >lose their value when you use them constantly, as would certainly
> >> >> >happen on TV.
>
> >> >> Eddie Murphy made a fortune by using them
> >> >> in high volumes
>
> >> >> Of course he was a *talented* cusser ... :-)
>
> >> >He was also funny at one time. Much like Carlin and Robin Williams,
> >> >they were all actually funnier when they were clean.
>
> >>    Actually, they all got really BORING when they
> >>    were 'clean' .... of illegal molecules.
>
> >Carlin wasn't. Robin Williams was manic with or without illegal
> >molecules.
>
>    Manic-depressive .... I won't watch any of his movies
>    anymore. They all abruptly fall into some deep dark
>    hole somewhere

Only the ones about the photo place and What Dreams May
Come, which is an absolutely fabulous movie that shouldn't have
had him in it.

>
>    He was funnier on dope. Some artistic types are just
>    like that.

I would say I represent that remark, but I'm neither artistic nor
on dope. it does nothing for me.

>
> >Eddie Murphy seemed to get boring after he had kids. Go figure.
>
>    Well ... I think he decided he was somehow obligated
>    to make a bunch of "kiddie films". Dunno if they were
>    meant to be presents to the kids, or just an egomaniacal
>    indulgence to show the kids Daddy was a MOVIE STAR.

He said something about this at one point. Something about wanting
to do movies he could watch with his kids. Okay, I get this, I have
kids
(not that young, but still kids). Still .. he wasn't quite as funny.

> >> >They just bowed
> >> >to the pressure of society. Look at Bill Cosby, who can be hysterical,
> >> >without ever using an improper word.
>
> >>    Used to be pretty funny ... has kinda drifted towards
> >>    being a sanctimonius shit in his senile years - sorta
> >>    like Bono.
>
> >>    But cuss-less comedy was Cosbys CHOICE. HE wanted to
> >>    do it that way for his own reasons. We've been discussing
> >>    people who use State authority to DENY choices, people
> >>    who think a little 'establishment' is a great thing,
> >>    people who'll tell you to say only what THEY want said -
> >>    or they'll have some guys with guns come and take you
> >>    away somewhere.
>
> >As I said, I agree that choice is a good thing. I just think that it
> >is
> >a rather stupid thing. I'm also a legalize drug believer, in spite of
> >the
> >fact that I don't do drugs, don't want to do drugs, and wouldn't do
> >them
> >if they were legal. I don't smoke either, but have no problem with
> >those that
> >do (as long as they don't do it in my face).
>
>    Well, the 'drug war' has proven itself to have far
>    worse and broader consequences than what it proports
>    to fight .... just leave the dopeheads to their own
>    devices and leave 'em a few paths OUT in case they
>    get tired of being stoned all the time. Clean,
>    safe(-ish) affordable dope would slash the crime
>    aspect to the bone as well.

Legalize it, tax it. I think we agree here.

>
> >America is pretty open about most things, aside from
> >religion and class warfare.
>
>    Unfortunately 'religion' can be expanded to cover
>    almost *everything* ... and class-warfare fills in
>    the gaps.

I believe in Reginald the Impure. He says we should wipe out
all Republicans. Hey, its a religion....

>
>    Americans LOVE a crusade - whether it's against
>    booze or dope or 'filth' or 'commies' or 'fascists'
>    or 'poverty' or 'wealth' or the 'wrong' religion ....
>    and there NEVER seems to be any shortage of shady
>    characters eager to exploit this love-affair,
>    ultimately to their own personal advantage.
>
>    Neither this quirk of human nature OR those who eagerly
>    exploit it are NEW things ... seem to go back at least
>    as far as, well, when folks had this idea about scribbling
>    shapes on damp clay tablets that had agreed-upon meanings.
>    The really shameful part is that even after ten thousand
>    years of being fooled, of being screwed-over by the
>    exploiters, We The People STILL haven't learned our lesson.
>    Every generation falls for pretty much the same old shit
>    (artistically repackaged, of course).

Have I mentioned you scare me sometimes?

Matt