using __getitem()__ correctly

Steven D'Aprano steve at pearwood.info
Thu Dec 31 10:03:20 EST 2015


On Thu, 31 Dec 2015 10:30 pm, Charles T. Smith wrote:

> On Thu, 31 Dec 2015 11:21:59 +1100, Ben Finney wrote:
> 
>> Steven D'Aprano <steve at pearwood.info> writes:
>> 
>>> On Thu, 31 Dec 2015 10:13 am, Ben Finney wrote:
>>>
>>> > You may be familiar with other languages where the distinction
>>> > between “attribute of an object” is not distinct from “item in a
>>> > dictionary”. Python is not one of those languages; the distinction is
>>> > real and important.
> ...
>> 
>> Tersely: the relationship between an object and its attributes, is not
>> the same as the relationship between a dictionary and its items.
> 
> 
> I understand this to mean that the relationship between a dictionary and
> its items is less complex than the relationship between an object and
> its attributes.

I think that is a fair comment, since attribute access involves MUCH more
complexity than dict item access. Attribute access involves one *or more*
dict access, *plus* a whole lot of extra complexity, while dict access by
definition involves only a single dict access.


> I'd like to catalog the different attribute types/implications:
> - perhaps a hierarchy of meta-ism - or user-relevance
> - those things in the __dict__
> - those things not in the __dict__ but without the "__"
> - ???

I don't discourage you from learning for the sake of learning, but how does
this get you closer to solving your actual problem?


>>> Obviously there is a syntax difference between x.attr and x['key']
>> 
>> Not merely syntax; the attributes of an object are not generally
>> available as items of the container.

I've lost track of who you are quoting here. I think Ben?


> What are the set of ways that an attribute is accessible?  Including
> implementation implications?

You can write:

x.attr

which is equivalent to calling the function:

getattr(x, "attr")


But really, the only limit is what you program x to do. Python gives you the
tools to make (say):

    x.what_is_my_name()[-1] + None

return x.attr, although why would you want to?

So let's put aside all the infinite number of weird and wacky ways that you
could, with sufficient work, access attributes, and consider only the
natural ways to do so. There are two:

x.attr
getattr(x, "attr")


>>> Either the instance __dict__, the class __dict__, or a superclass
>>> __dict__.
>> 
>> No, I'm not referring to the ‘__dict__’ attribute of an object; I'm
>> referring to the object itself.
>> 
>> To talk about the attributes of an object ‘foo’ is distinct from talking
>> about the items in a dictionary ‘foo’. That distinction is real, and
>> important.
> 
> 
> But wanting to deal with the attributes of an object without considering
> the way it's implemented - although possible - requires a complete virtual
> model that covers all implications.  It's easier to simply understand how
> the objects work under the covers.

You should understand how attributes are implemented, but it is not
necessary to understand it in all the gory detail just to use them. Now I'm
feeling some sympathy for the people on StackOverflow who told you that you
don't need to understand this stuff -- perhaps I was too harsh on them :-)

There's a lot of complicated implementation detail involved in attribute
access, but for 99% of uses that's all handled for you and all you need do
is write x.attr.


-- 
Steven




More information about the Python-list mailing list