Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > no-clobber dicts?

Reply
Thread Tools

no-clobber dicts?

 
 
Raymond Hettinger
Guest
Posts: n/a
 
      08-04-2009
[kj]
> The implication here is that .update() does not in turn use
> .__setitem__(), which I find a bit surprising.


It's never wise to make assumptions about this sort of thing.
Every method in a class or type is allowed to directly access
or modify its private, internal data. The implementation is
free to choose whether to limit that access to a few accessors
(i.e. getitem, setitem, delitem, len) or to let all of the
methods have direct access to the underlying data structure.
Even if accessors were used, the choice would be arbitrary
(perhaps delitem() gets implemented in terms of pop() or somesuch).

This is a basic for OO programming. Unless the class documentation
promises to use particular hooks, the implementation is free to
change in any way that doesn't violate the published API (this is
one of the benefits of encapsulation and abstract data types).

In general, if a subclass is overriding, rather than extending,
then it should override *every* method that could be affected.


Raymond


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-05-2009
On Wed, 5 Aug 2009 06:01 am kj wrote:

> In <(E-Mail Removed) om.au> Steven
> D'Aprano <(E-Mail Removed)> writes:
>
>>class ConstantNamespace(dict):

> <snip>
>
>>I also have a series of unit tests for it if you're interested in them.

>
> Actually, come to think of it, I think I'll take you up on this.
> I'd love to see those tests. Unit testing in Python is in area I
> need to work on.



No problem. Here you go:

http://www.cybersource.com.au/users/...n/constants.py

Comments welcome.



--
Steven
 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      08-05-2009
In <00027aa9$0$2969$(E-Mail Removed)> Steven D'Aprano <(E-Mail Removed)> writes:

>No problem. Here you go:


>http://www.cybersource.com.au/users/...n/constants.py


Extremely helpful. Thanks!

kynn
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments