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


Groups > comp.lang.python > #20926

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!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'c++,': 0.07; 'python': 0.08; '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; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'this:': 0.16; '(i.e.': 0.17; 'wrote:': 0.18; 'instance': 0.18; 'working.': 0.18; 'say,': 0.19; '(which': 0.19; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'feb': 0.22; 'assigning': 0.23; 'dictionary': 0.23; 'fine,': 0.23; 'received:209.85.220': 0.25; 'cc:2**0': 0.26; 'work.': 0.27; 'function': 0.27; 'effect': 0.28; 'message- id:@mail.gmail.com': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; '(and': 0.30; 'object.': 0.30; 'sun,': 0.30; 'chris': 0.30; 'creates': 0.31; 'subject:?': 0.31; 'rather': 0.34; 'assignment': 0.34; 'function.': 0.34; 'something': 0.35; 'subject:How': 0.35; 'subject:can': 0.35; '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': 0.39; 'being': 0.40; 'john': 0.61; 'header:Received:6': 0.61; 'custom': 0.61; 'believe': 0.65; 'subject:like': 0.67; 'url:htm': 0.69; '26,': 0.73; 'subject:make': 0.73; '11:24': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84
Received-SPF pass (google.com: domain of chris@rebertia.com designates 10.52.67.229 as permitted sender) client-ip=10.52.67.229;
Authentication-Results mr.google.com; spf=pass (google.com: domain of chris@rebertia.com designates 10.52.67.229 as permitted sender) smtp.mail=chris@rebertia.com; dkim=pass header.i=chris@rebertia.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=Ln5HGN1+bt7o+lYcyQObdVnWv9QOSA2k7wFOaQ//Jj4=; b=AQGK2sFcmmroC30GNwwhl8XLDK2XydRSFYcIKYQW4kakW51ilx9FJcYAjrr/ah/AKd p5dNKRnG1J4YXdaboTJhzMGlI2nnLoW3v2MkrW1fWsibPnq7noNB45mR1mu+7oxPWonq 0ChRn8qeGSmnS3PDH/NMBVShb4ety/tJRaBcY=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <a48bde30-31ed-45a9-ba89-6559c17d0992@y17g2000yqg.googlegroups.com>
References <a48bde30-31ed-45a9-ba89-6559c17d0992@y17g2000yqg.googlegroups.com>
Date Sun, 26 Feb 2012 23:39:01 -0800
X-Google-Sender-Auth fio2ENw0dACQt29Rhf4zXoDyUxM
Subject Re: How can I make an instance of a class act like a dictionary?
From Chris Rebert <clp2@rebertia.com>
To John Salerno <johnjsal@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Gm-Message-State ALoCoQkqBegAgPagfZ5xtuujNuPyKxvSxDKAh7fzez6l1rmD5EDO4lannNFxgNY5nzTzuJYYHyW4
Cc python-list@python.org
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.190.1330328350.3037.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330328350 news.xs4all.nl 6870 [2001:888:2000:d::a6]:37108
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20926

Show key headers only | View raw


On Sun, Feb 26, 2012 at 11:24 PM, John Salerno <johnjsal@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

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