Re: using __getitem()__ correctly

2015-12-31 Thread Steven D'Aprano
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 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

Re: using __getitem()__ correctly

2015-12-31 Thread Steven D'Aprano
Hmmm, you seem to be pasting in text from multiple messages, and jumping around in time ("Ian had said, to which I answered") which may get a bit confusing. Hopefully I can answer without getting lost :-) On Thu, 31 Dec 2015 10:17 pm, Charles T. Smith wrote: > On Thu, 31 Dec 2015 10:50:53 +1100,

Re: using __getitem()__ correctly

2015-12-31 Thread Charles T. Smith
On Thu, 31 Dec 2015 12:12:43 +, Oscar Benjamin wrote: > When you write x.attr the name 'attr' is looked up on the object x. This > calls x.__getattribute__('attr'). In turn this checks the dict > associated with the object x i.e. x.__dict__['attr']. This in turn calls > x.__dict__.__getitem__

Re: using __getitem()__ correctly

2015-12-31 Thread Charles T. Smith
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 othe

Re: using __getitem()__ correctly

2015-12-31 Thread Oscar Benjamin
On 31 December 2015 at 11:30, Charles T. Smith wrote: >>> 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. > > > What are the set of ways that an attribute is accessible

Re: using __getitem()__ correctly

2015-12-31 Thread Ben Finney
"Charles T. Smith" writes: > On Thu, 31 Dec 2015 11:21:59 +1100, Ben Finney wrote: > > > 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 dictio

Re: using __getitem()__ correctly

2015-12-31 Thread Charles T. Smith
On Thu, 31 Dec 2015 11:21:59 +1100, Ben Finney wrote: > Steven D'Aprano 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”. Pyth

Re: using __getitem()__ correctly

2015-12-31 Thread Charles T. Smith
On Thu, 31 Dec 2015 10:50:53 +1100, Steven D'Aprano wrote: > I'm not sure what distinction you're referring to, can you explain? Ian Kelly had said: >> How precisely are you trying to store these: as an attribute, or as a >> dict item? If it's supposed to be in the dict, then why is your >> __ge

Re: using __getitem()__ correctly

2015-12-30 Thread Ian Kelly
On Wed, Dec 30, 2015 at 3:54 PM, Charles T. Smith wrote: > On Wed, 30 Dec 2015 13:40:44 -0700, Ian Kelly wrote: > >> On Wed, Dec 30, 2015 at 9:58 AM, Charles T. Smith >>> The problem is that then triggers the __getitem__() method and I don't >>> know how to get to the attributes without triggering

Re: using __getitem()__ correctly

2015-12-30 Thread Ben Finney
Steven D'Aprano 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 an

Re: using __getitem()__ correctly

2015-12-30 Thread Steven D'Aprano
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. I'm not sure what dist

Re: using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
On Thu, 31 Dec 2015 10:13:53 +1100, Ben Finney wrote: > "Charles T. Smith" writes: > >> I don't understand this distinction between an "attribute" and a "dict >> item". > > When did you most recently work through the Python tutorial > https://docs.python.org/3/tutorial/>> You may want to work t

Re: using __getitem()__ correctly

2015-12-30 Thread Ben Finney
"Charles T. Smith" writes: > I don't understand this distinction between an "attribute" and a "dict > item". When did you most recently work through the Python tutorial https://docs.python.org/3/tutorial/>> You may want to work through it again, from start to finish and exercising each example,

Re: using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
On Wed, 30 Dec 2015 22:54:44 +, Charles T. Smith wrote: > But I concede I must be doing something fundamentally wrong because this > assert is triggering: > def __getattr__ (self, name): > print "attrdict:av:__getattr__: entered for ", name > assert name not in self.keys(),

Re: using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
On Wed, 30 Dec 2015 13:40:44 -0700, Ian Kelly wrote: > On Wed, Dec 30, 2015 at 9:58 AM, Charles T. Smith >> The problem is that then triggers the __getitem__() method and I don't >> know how to get to the attributes without triggering __getattr__(). >> >> It's the interplay of the two that's killi

Re: using __getitem()__ correctly

2015-12-30 Thread Ian Kelly
On Wed, Dec 30, 2015 at 9:58 AM, Charles T. Smith wrote: > On Wed, 30 Dec 2015 08:35:57 -0700, Ian Kelly wrote: > >> On Dec 30, 2015 7:46 AM, "Charles T. Smith" >> wrote: >>> As is so often the case, in composing my answer to your question, I >>> discovered a number of problems in my class (e.g.

Re: using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
On Wed, 30 Dec 2015 08:35:57 -0700, Ian Kelly wrote: > On Dec 30, 2015 7:46 AM, "Charles T. Smith" > wrote: >> As is so often the case, in composing my answer to your question, I >> discovered a number of problems in my class (e.g. I was calling >> __getitem__() myself!), but I'm puzzled now how

Re: using __getitem()__ correctly

2015-12-30 Thread Ian Kelly
On Dec 30, 2015 7:46 AM, "Charles T. Smith" wrote: > As is so often the case, in composing my answer to your question, I discovered > a number of problems in my class (e.g. I was calling __getitem__() myself!), > but > I'm puzzled now how to proceed. I thought the way you avoid triggering > __g

Re: using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
On Thu, 31 Dec 2015 00:11:24 +1100, Chris Angelico wrote: > On Wed, Dec 30, 2015 at 11:57 PM, Charles T. Smith > wrote: >> Hello, >> >> I thought __getitem__() was invoked when an object is postfixed with an >> expression in brackets: >> >> - abc[n] >> >> and __getattr__() was invoked when an o

Re: using __getitem()__ correctly

2015-12-30 Thread Chris Angelico
On Wed, Dec 30, 2015 at 11:57 PM, Charles T. Smith wrote: > Hello, > > I thought __getitem__() was invoked when an object is postfixed with an > expression in brackets: > > - abc[n] > > and __getattr__() was invoked when an object is postfixed with an dot: > > - abc.member That would be norma

using __getitem()__ correctly

2015-12-30 Thread Charles T. Smith
Hello, I thought __getitem__() was invoked when an object is postfixed with an expression in brackets: - abc[n] and __getattr__() was invoked when an object is postfixed with an dot: - abc.member but my __getitem__ is being invoked at this time, where there's no subscript (going into a s