Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47591

Re: Newbie: question regarding references and class relationships

Date 2013-06-10 15:36 -0400
From Dave Angel <davea@davea.name>
Subject Re: Newbie: question regarding references and class relationships
References (6 earlier) <kp4qi6$dno$1@dont-email.me> <mailman.2972.1370879753.3114.python-list@python.org> <kp4tf4$1on$1@dont-email.me> <mailman.2975.1370884029.3114.python-list@python.org> <kp52tm$455$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.2980.1370893004.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 06/10/2013 01:42 PM, Rui Maciel wrote:
> Peter Otten wrote:
>
>> Have you read the code in the interpreter session I posted?
>>
>> If you do not agree that the demonstrated behaviour is puzzling I'll have
>> to drop my claim...
>
> I don't see how it should be puzzling.  You've deleted the attribute, so it
> ceassed to exist.

Clearly you didn't reason through all the code in Peter's example.

>
>
>> Likewise if you can show a benefit of the
>>
>>>> position = []
>>
>> line.
>
> I wrote the code that way to declare intent and help document the code.  In
> this case that the class Point is expected to have an attribute named
> position which will point to a list.


So why do you also have an instance attribute of the same name?  If you 
really want all instances to have the same value for position, you'd 
better change the __init__() function so it doesn't mask that single value.

Could it be that you really meant that instances of class Point are each 
expected to have an attribute named Position?  Could it be that you want 
each instance's position to be independent of the others?

By having a class attribute with the same name as the instance 
attribute, you're occasionally going to use the class version when you 
meant the instance one.

I suspect you didn't realize the distinction between class attributes 
and instance attributes, and that a comment would be a much better way 
to communicate than creating a misleading value.

In one of your other messages, you asked:

 > How do you guarantee that any object of a class has a
 >  specific set of attributes?

Answer is to define those INSTANCE attributes in the __init__() method 
(or occasionally in the __new__() method), and to make sure you don't 
ever delete them.  The attributes of the object are instance attributes, 
while the attributes defined inside the class are class attributes.

Occasionally, it can be useful to let a class attribute be a 'backup' to 
the instance attributes, but you've got to think through your use case. 
  If memory is really tight, and if nearly all of the instances want the 
same value, then you could omit the instance attribute except for the 
exceptional ones, and let the class attribute fill in.  But as Peter 
says, if it's mutable, you then open yourself to somebody changing them 
all, thinking it was only changing the one.  But there are good reasons 
not to use this trick to save memory.


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:18 +0100
  Re: Newbie: question regarding references and class relationships Roy Smith <roy@panix.com> - 2013-06-10 09:35 -0400
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:57 +0100
      Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:00 +0100
      Re: Newbie: question regarding references and class relationships Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-10 13:56 -0600
        Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 17:02 -0700
  Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 15:49 +0200
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:59 +0100
      Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 16:28 +0200
        Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:38 +0100
          Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:05 +0200
            Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 16:20 +0100
              Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:55 +0200
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 17:09 +0100
                Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 19:07 +0200
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 18:42 +0100
                Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 15:36 -0400
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:31 +0100
                Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:51 -0400
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:13 +0100
                Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 18:07 -0400
                Re: Newbie: question regarding references and class relationships Grant Edwards <invalid@invalid.invalid> - 2013-06-10 22:39 +0000
                Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-11 08:54 +1000
                Re: Newbie: question regarding references and class relationships Tim Chase <python.list@tim.thechases.com> - 2013-06-10 18:24 -0500
                Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 19:26 -0400
                Re: Newbie: question regarding references and class relationships Jason Swails <jason.swails@gmail.com> - 2013-06-10 23:49 -0400
  Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:54 -0400
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:09 +0100
  Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 16:50 -0700
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 13:06 +0100
      Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-14 00:07 +1000
        Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 19:23 +0100

csiph-web