storing meta data on dictionary keys

Andreas Kraemer akraemer at sbcglobal.net
Thu Oct 11 21:25:49 CEST 2007


On Oct 11, 10:17 am, Erik Jones <e... at myemma.com> wrote:

> No, duck typing and inheritance are two different things.  Duck
> typing is when you implement the same operations as another object or
> class, whereas with inheritance you get the same implementation as
> that of the parent class.

Except when you override a method ...

> With duck typing, an object "acts like"
> another object in specific contexts.

That's exactly what Str(str) does in the context dictionary look-
up ...

> With inheritance, an object is
> the same general type as another object.  You don't want string
> objects, you want objects that, in a specific case, act like strings
> but in others, I'm sure, not.  How does a concept like string
> substitution fit with your inherited objects?

Since Str(str) inherits all methods of str, those methods that return
other strings (always new objects since str is immutable) like e.g.
replace() will return str and not Str(str), and don't have the meta
data copied. Which may be a bug or a feature  ...:-)

I think the subtle difference here lies between the definition of
inheritance and duck-typing, and how both are typically used in
practice ...

> If you can, get a copy of Beautiful Code (easily had from Amazon or
> the likes).  It contains an chapter on Python's dictionary
> implementation.

Thanks for the pointer. I like to look at beautiful code.




More information about the Python-list mailing list