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


Groups > comp.lang.python > #66093

Re: Simple Object assignment giving me errors

References <d7d2865e-4184-4c05-b1bd-b4f16547b2f2@googlegroups.com>
Date 2014-02-13 08:26 +1100
Subject Re: Simple Object assignment giving me errors
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6790.1392240405.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 13, 2014 at 8:18 AM, Nir <nirchernia@gmail.com> wrote:
> class FileInfo(UserDict):
>         def __init__(self, filename=None):
>                 UserDict.__init__(self)
>                 self["name"] = filename
>
> I get a TypeError: 'FileInfo' object doesn't support item assignment .
>
> Am I missing something?

You can't use square-brackets notation like that, unless you've
written your class specifically to handle it. More likely, what you
want is one of:

self.name = filename
self.dict["name"] = filename

Also, the same problem will occur with the UserDict, which tries to
update itself rather than its dict.

Actually, a simpler solution might be to have UserDict inherit from
dict. I'm not sure what you're trying to achieve here; more detail
would help. But if UserDict really is a dict, then you can call
self.update, and you can use square-brackets item assignment. I've no
idea what you'd gain over just using a dict though.

ChrisA

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


Thread

Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 13:18 -0800
  Re: Simple Object assignment giving me errors Chris Angelico <rosuav@gmail.com> - 2014-02-13 08:26 +1100
  Re:Simple Object assignment giving me errors Dave Angel <davea@davea.name> - 2014-02-12 16:41 -0500
  Re: Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 13:42 -0800
    Re: Simple Object assignment giving me errors Chris Angelico <rosuav@gmail.com> - 2014-02-13 08:48 +1100
    Re: Simple Object assignment giving me errors Jerry Hill <malaclypse2@gmail.com> - 2014-02-12 17:09 -0500
  Re: Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 14:25 -0800

csiph-web