__getitem__ method on (meta)classes

Ron Garret rNOSPAMon at flownet.com
Wed Mar 16 12:27:46 EST 2005


In article <2IidnaIwIseSdqrfRVn-2A at comcast.com>,
 Steven Bethard <steven.bethard at gmail.com> wrote:

> Ron Garret wrote:
> > In article <R4idnd0F2IMyvqrfRVn-1g at comcast.com>,
> >  Steven Bethard <steven.bethard at gmail.com> wrote:
> > 
> >>>Yeah, except I actually left out one thing:  I also want type(v)==e1.
> >>
> >>Why?  In Python usually you rely on duck-typing and not explicit type 
> >>checks.  What is it that you're trying to gain by asserting type(v) == e1?
> > 
> > Clarity.  I want to be able to distinguish a member of an enumeration 
> > from a string or an integer for the same reason one would want to 
> > distinguish 123 from "123".
> 
> So then you don't necessarily need that type(v) == e1, you just need 
> type(v) != str.  How about:

[xnip]

That's not bad.  This conflates all enumerated items into a single 
namespace whereas the metaclass solution makes each enumeration its own 
namespace.  For example:

plumbers = enum['Fred','John','Bill']
electricians = enum['Fred','Jake','Lester']

Using a metaclass allows you to distinguish Fred the plumber from Fred 
the electrician.  But that may or may not be what one actually wants.

Must sleep on it.

rg



More information about the Python-list mailing list