Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'interpreter': 0.05; 'position,': 0.05; 'attribute': 0.07; 'memory.': 0.07; 'attributes': 0.09; 'mask': 0.09; 'name?': 0.09; 'occasionally': 0.09; 'omit': 0.09; 'subject:question': 0.10; 'wrote': 0.14; 'attribute,': 0.16; 'attributes,': 0.16; 'attributes.': 0.16; 'exist.': 0.16; 'likewise': 0.16; 'received:74.208.4.195': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'value.': 0.19; 'meant': 0.20; 'memory': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'example.': 0.24; '(or': 0.24; 'define': 0.26; 'nearly': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; "doesn't": 0.30; 'code': 0.31; '>>>>': 0.31; 'class': 0.32; 'open': 0.33; 'comment': 0.34; 'could': 0.34; 'agree': 0.35; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'in.': 0.36; 'instances': 0.36; 'ones,': 0.36; 'subject:regarding': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; "i'll": 0.36; 'should': 0.36; 'changing': 0.37; 'list.': 0.37; 'expected': 0.38; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'realize': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; "you're": 0.61; 'save': 0.62; "you've": 0.63; 'guarantee': 0.63; 'show': 0.63; 'name': 0.63; 'between': 0.67; 'benefit': 0.68; 'received:74.208': 0.68; 'yourself': 0.78; 'otten': 0.84; 'tight,': 0.84 Date: Mon, 10 Jun 2013 15:36:24 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Newbie: question regarding references and class relationships References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:gw9XPkd+247QzoX+h1Emyfue9pxK3YsEhhVVq8014TC jt6PuJ5HjVS9d9eYrc2Ym0THRM3Vz5RbYJciY3mu/ZtFl0ordM 7DwaQtv1/J8eWZpb9cTTTxq/meTmcScBkTSsJYaemQ3R9gH8Vg 9fTrZqM7IydGBWlYcFIGFOcfjT6ygzRBrzhkEzKboEmBTjSewL DTpqAV4jBKQbiMMopGVUtxAPMZ0sX5DEJLo0jOvviorxAWzHT5 dHj23w2bpfpKexmrrhILd5k7R0gllrAaoznczalWYuzCbHFPvn f0FWmoVbTH0MfKFIrvgdXma3zIaN8hMycoOjkwO3ZK6JlIekw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370893004 news.xs4all.nl 15964 [2001:888:2000:d::a6]:58801 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47591 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