Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python,': 0.01; 'value,': 0.04; 'c++,': 0.07; 'working:': 0.07; 'python': 0.08; 'foo': 0.09; 'modifies': 0.09; 'wrong,': 0.09; 'def': 0.13; 'argument': 0.15; 'class,': 0.15; '__init__': 0.16; 'dictionary,': 0.16; 'needless': 0.16; 'subject:instance': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'variable.': 0.16; 'mon,': 0.16; 'syntax': 0.16; 'this:': 0.16; 'meant': 0.17; '(i.e.': 0.17; 'wrote:': 0.18; 'instance': 0.18; 'working.': 0.18; 'say,': 0.19; '(which': 0.19; 'cheers,': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'assigning': 0.23; 'dictionary': 0.23; 'fine,': 0.23; 'thanks.': 0.24; 'work.': 0.27; 'url:mailman': 0.27; 'function': 0.27; 'effect': 0.28; 'message-id:@mail.gmail.com': 0.29; '27,': 0.29; 'class': 0.29; 'pm,': 0.29; '(and': 0.30; 'none,': 0.30; 'object.': 0.30; 'sun,': 0.30; 'chris': 0.30; "didn't": 0.30; 'creates': 0.31; 'subject:?': 0.31; 'actually': 0.31; 'url:listinfo': 0.32; 'does': 0.32; 'assigned': 0.32; 'objects': 0.32; "isn't": 0.33; 'object': 0.33; 'rather': 0.34; 'setting': 0.34; 'header:Received:8': 0.34; 'anything': 0.34; 'assignment': 0.34; 'function.': 0.34; 'to:addr:python-list': 0.35; 'none': 0.35; 'something': 0.35; 'subject:How': 0.35; 'subject:can': 0.35; 'url:python': 0.35; 'do?': 0.37; 'everyone.': 0.37; 'created': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'uses': 0.38; 'could': 0.38; 'doing': 0.38; 'getting': 0.38; 'url:org': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'being': 0.40; 'change': 0.40; 'to:addr:python.org': 0.40; 'john': 0.61; 'custom': 0.61; 'matter': 0.62; 'here': 0.64; 'believe': 0.65; 'subject:like': 0.67; 'url:htm': 0.69; 'anything.': 0.71; '26,': 0.73; 'subject:make': 0.73; '--->': 0.84; '11:24': 0.84; '3:09': 0.84; 'case?': 0.84; 'received:10.152': 0.84; 'url:rebertia': 0.84 Received-SPF: pass (google.com: domain of bsk16@case.edu designates 10.152.135.104 as permitted sender) client-ip=10.152.135.104; Authentication-Results: mr.google.com; spf=pass (google.com: domain of bsk16@case.edu designates 10.152.135.104 as permitted sender) smtp.mail=bsk16@case.edu MIME-Version: 1.0 In-Reply-To: <007de874-f643-4092-8df0-69320795fc14@b23g2000yqn.googlegroups.com> References: <007de874-f643-4092-8df0-69320795fc14@b23g2000yqn.googlegroups.com> Date: Mon, 27 Feb 2012 15:37:15 -0500 Subject: Re: How can I make an instance of a class act like a dictionary? From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQm6LON8EUm4A6kTs2vid6HWDrG6UhdTERqlM4ZMlNjEA4KqsSwB/uXeQA7J8xGBUUIbroSZ X-Junkmail-Whitelist: YES (by domain whitelist at mpv1.tis.cwru.edu) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330375047 news.xs4all.nl 6941 [2001:888:2000:d::a6]:48427 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20951 On Mon, Feb 27, 2012 at 3:09 PM, John Salerno wrote: > On Feb 27, 1:39=A0am, Chris Rebert wrote: >> On Sun, Feb 26, 2012 at 11:24 PM, John Salerno wrot= e: >> > Hi everyone. I created a custom class and had it inherit from the >> > "dict" class, and then I have an __init__ method like this: >> >> > def __init__(self): >> > =A0 =A0 =A0 =A0self =3D create() >> >> > The create function creates and returns a dictionary object. Needless >> > to say, this is not working. When I create an instance of the above >> > class, it is simply an empty dictionary rather than the populated >> > dictionary being created by the create function. Am I doing the >> > inheritance wrong, or am I getting the above syntax wrong by assigning >> > the return value to self? >> >> Assignment to `self` has no effect outside the method in question; >> Python uses call-by-object (http://effbot.org/zone/call-by-object.htm >> ) for argument passing. >> Even in something like C++, I believe assignment to `this` doesn't work. >> >> > I know I could do self.variable =3D create() and that works fine, but = I >> > thought it would be better (and cleaner) simply to use the instance >> > itself as the dictionary, rather than have to go through an instance >> > variable. >> >> Call the superclass (i.e. dict's) initializer (which you ought to be >> doing anyway): >> =A0 =A0 super(YourClass, self).__init__(create()) >> >> Cheers, >> Chris >> --http://rebertia.com > > Thanks. This ended up working: > > def __init__(self): > =A0 =A0 =A0 =A0self =3D super().__init__(create_board()) > > Is that what you meant for me to do? Why did assigning to self work in > this case, but not the original case? It didn't do anything and still isn't doing anything. In Python, names are assigned to objects self -> ^ foo -------| Reassigning a name does not change the value, so self =3D create() Just makes an object2 self -> foo ---> The reason it's working here is because the super().__init__() call modifies the existing object in place. It returns None, so you're setting self =3D None but that doesn't matter because of what I explained before about how assigning to self doesn't actually change anything. > -- > http://mail.python.org/mailman/listinfo/python-list