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


Groups > comp.lang.python > #20951

Re: How can I make an instance of a class act like a dictionary?

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 <bsk16@case.edu>
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 <a48bde30-31ed-45a9-ba89-6559c17d0992@y17g2000yqg.googlegroups.com> <mailman.190.1330328350.3037.python-list@python.org> <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 <benjamin.kaplan@case.edu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.209.1330375047.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Feb 27, 2012 at 3:09 PM, John Salerno <johnjsal@gmail.com> wrote:
> On Feb 27, 1:39 am, Chris Rebert <c...@rebertia.com> wrote:
>> On Sun, Feb 26, 2012 at 11:24 PM, John Salerno <johnj...@gmail.com> wrote:
>> > 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):
>> >        self = 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 = 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):
>>     super(YourClass, self).__init__(create())
>>
>> Cheers,
>> Chris
>> --http://rebertia.com
>
> Thanks. This ended up working:
>
> def __init__(self):
>        self = 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 -> <Object1>
              ^
foo -------|

Reassigning a name does not change the value, so
self = create()

Just makes an object2

self -> <Object2>

foo ---> <Object1>

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 = 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

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


Thread

How can I make an instance of a class act like a dictionary? John Salerno <johnjsal@gmail.com> - 2012-02-26 23:24 -0800
  Re: How can I make an instance of a class act like a dictionary? Chris Rebert <clp2@rebertia.com> - 2012-02-26 23:39 -0800
    Re: How can I make an instance of a class act like a dictionary? John Salerno <johnjsal@gmail.com> - 2012-02-27 12:09 -0800
      Re: How can I make an instance of a class act like a dictionary? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-02-27 15:37 -0500
  Re: How can I make an instance of a class act like a dictionary? Dan Sommers <dan@tombstonezero.net> - 2012-02-27 10:52 +0000

csiph-web