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


Groups > comp.lang.python > #20925 > unrolled thread

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

Started byJohn Salerno <johnjsal@gmail.com>
First post2012-02-26 23:24 -0800
Last post2012-02-27 10:52 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#20925 — How can I make an instance of a class act like a dictionary?

FromJohn Salerno <johnjsal@gmail.com>
Date2012-02-26 23:24 -0800
SubjectHow can I make an instance of a class act like a dictionary?
Message-ID<a48bde30-31ed-45a9-ba89-6559c17d0992@y17g2000yqg.googlegroups.com>
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?

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.

Thanks.

[toc] | [next] | [standalone]


#20926

FromChris Rebert <clp2@rebertia.com>
Date2012-02-26 23:39 -0800
Message-ID<mailman.190.1330328350.3037.python-list@python.org>
In reply to#20925
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

[toc] | [prev] | [next] | [standalone]


#20950

FromJohn Salerno <johnjsal@gmail.com>
Date2012-02-27 12:09 -0800
Message-ID<007de874-f643-4092-8df0-69320795fc14@b23g2000yqn.googlegroups.com>
In reply to#20926
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?

[toc] | [prev] | [next] | [standalone]


#20951

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2012-02-27 15:37 -0500
Message-ID<mailman.209.1330375047.3037.python-list@python.org>
In reply to#20950
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

[toc] | [prev] | [next] | [standalone]


#20932

FromDan Sommers <dan@tombstonezero.net>
Date2012-02-27 10:52 +0000
Message-ID<mailman.191.1330339962.3037.python-list@python.org>
In reply to#20925
On Sun, 26 Feb 2012 23:24:31 -0800, John Salerno 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:

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

Check out the "Bunch" class:

    http://code.activestate.com/recipes/52308/

HTH,
Dan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web