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

Discussion in 'Python' started by John Salerno, Feb 27, 2012.

  1. John Salerno

    John Salerno Guest

    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.
    John Salerno, Feb 27, 2012
    #1
    1. Advertising

  2. John Salerno

    Chris Rebert Guest

    On Sun, Feb 26, 2012 at 11:24 PM, 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:
    >
    > 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
    Chris Rebert, Feb 27, 2012
    #2
    1. Advertising

  3. John Salerno

    Dan Sommers Guest

    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
    Dan Sommers, Feb 27, 2012
    #3
  4. John Salerno

    John Salerno Guest

    On Feb 27, 1:39 am, Chris Rebert <> wrote:
    > On Sun, Feb 26, 2012 at 11:24 PM, 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:

    >
    > > 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?
    John Salerno, Feb 27, 2012
    #4
  5. On Mon, Feb 27, 2012 at 3:09 PM, John Salerno <> wrote:
    > On Feb 27, 1:39 am, Chris Rebert <> wrote:
    >> On Sun, Feb 26, 2012 at 11:24 PM, 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:

    >>
    >> > 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
    Benjamin Kaplan, Feb 27, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dipesh
    Replies:
    0
    Views:
    313
    dipesh
    Nov 9, 2004
  2. Edward
    Replies:
    3
    Views:
    898
    Edward
    Aug 4, 2006
  3. Lambda
    Replies:
    3
    Views:
    289
    Bruno Desthuilliers
    Sep 15, 2009
  4. active
    Replies:
    4
    Views:
    262
    active
    Apr 3, 2007
  5. Chuck W.

    Make checkboxes act like radiobuttons?

    Chuck W., Nov 11, 2005, in forum: Javascript
    Replies:
    8
    Views:
    89
    Chuck W.
    Nov 12, 2005
Loading...

Share This Page