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


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

using __getitem()__ correctly

Started by"Charles T. Smith" <cts.private.yahoo@gmail.com>
First post2015-12-30 12:57 +0000
Last post2015-12-31 12:45 +0000
Articles 1 on this page of 21 — 6 participants

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


Contents

  using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 12:57 +0000
    Re: using __getitem()__ correctly Chris Angelico <rosuav@gmail.com> - 2015-12-31 00:11 +1100
      Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 14:40 +0000
        Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 08:35 -0700
          Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 16:58 +0000
            Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 13:40 -0700
              Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 22:54 +0000
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 22:58 +0000
                Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 10:13 +1100
                  Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 23:18 +0000
                  Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2015-12-31 10:50 +1100
                    Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 11:21 +1100
                      Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 11:30 +0000
                        Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 22:51 +1100
                        Re: using __getitem()__ correctly Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 12:12 +0000
                          Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 13:39 +0000
                        Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2016-01-01 02:03 +1100
                    Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 11:17 +0000
                      Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2016-01-01 01:43 +1100
                Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 17:31 -0700
                  Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 12:45 +0000

Page 2 of 2 — ← Prev page 1 [2]


#101054

From"Charles T. Smith" <cts.private.yahoo@gmail.com>
Date2015-12-31 12:45 +0000
Message-ID<n63821$gli$4@dont-email.me>
In reply to#101031
On Wed, 30 Dec 2015 17:31:11 -0700, Ian Kelly wrote:

>> In any case, I thought that class attributes were, in fact, items of
>> __dict__?
> 
> That's correct, but as I said in my previous message, self.attrs and
> self.attrs.__dict__ are two different dicts, and you're confusing one
> for the other. Maybe this will be illuminating:
> 
>>>> class mydict(dict): pass
> ...
>>>> md = mydict()
>>>> md['foo'] = 42  # Set an item in md
>>>> md['foo']  # 'foo' exists as an item
> 42
>>>> md.foo  # but not as an attribute
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'mydict' object has no attribute 'foo'
>>>> md.__dict__['foo']  # and it's not in md.__dict__
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyError: 'foo'
>>>> md.bar = 43  # Set an attribute on md md.bar  # 'bar' exists as an
>>>> attribute
> 43
>>>> md.__dict__['bar']  # and it's in md.__dict__
> 43
>>>> md['bar']  # but it's not in md itself
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyError: 'bar'
> 
> And to hopefully drive the point home:
> 
>>>> md.items()
> [('foo', 42)]
>>>> md.__dict__.items()
> [('bar', 43)]


Okay, I think I got an important point that you said earlier but
I didn't get the full ramifications thereof:

  class md (... more about this ...):
      pass

  md is a class that has an dictionary attribute __dict__:
  - md['level1'] puts attributes in md
  - md.level2 puts attributes in md.__dict__

is that correct?

Now, as to the superclass, if that's, say, object, then
it has two levels:

  md -> object (containing a dict)

if the superclass is dict, then it only has one level:

  md -> dict

I'll have to look at my methods again with those understandings.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web