Re: __getitem__ method on (meta)classes

2005-03-16 Thread Steven Bethard
Ron Garret wrote: 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. Not sure what you mean here. The non metaclass solution still has separate enum.item objects for each new enum. Consider your implementa

Re: __getitem__ method on (meta)classes

2005-03-16 Thread Ron Garret
In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> wrote: > Ron Garret wrote: > > In article <[EMAIL PROTECTED]>, > > Steven Bethard <[EMAIL PROTECTED]> wrote: > > > >>>Yeah, except I actually left out one thing: I also want type(v)==e1. > >> > >>Why? In Python usually you rel

Re: __getitem__ method on (meta)classes

2005-03-16 Thread Steven Bethard
Steven Bethard wrote: py> class enum(object): ... class item(object): ... def __init__(self, val): ... self.val = val ... def __repr__(self): ... return 'enum.item(%r)' % self.val ... def __init__(self, vals): ... self.items = [type(self).item

Re: __getitem__ method on (meta)classes

2005-03-16 Thread Steven Bethard
Ron Garret wrote: In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> 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(

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Ron Garret
In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> 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) ==

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Bengt Richter
On Tue, 15 Mar 2005 08:32:51 -0800, Ron Garret <[EMAIL PROTECTED]> wrote: >In article <[EMAIL PROTECTED]>, > [EMAIL PROTECTED] (Bengt Richter) wrote: > >> >Did you mean type(x).__getitem__(x,y)? >> > >> Not if x is a classmethod, D'oh. I meant "not if __getitem__ is a classmethod" ;-P > >Oh yeah,

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Steven Bethard
Ron Garret wrote: In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> wrote: Ron Garret wrote: What I'm really trying to do is to create enumerated types such that if: e1 = enum(lst) and v = e1(x) then (x in lst) and (e1[v] == x) Use a class with __call__ and __getitem__: py> class

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Ron Garret
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Bengt Richter) wrote: > On Mon, 14 Mar 2005 23:44:46 -0700, Steven Bethard <[EMAIL PROTECTED]> > wrote: > > >Ron Garret wrote: > >> What I'm really trying to do is to create enumerated types such that if: > >> > >> e1 = enum(lst) and v = e1(x)

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Ron Garret
In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> wrote: > Ron Garret wrote: > > What I'm really trying to do is to create enumerated types such that if: > > > > e1 = enum(lst) and v = e1(x) > > > > then > > > > (x in lst) and (e1[v] == x) > > Use a class with __call__ and __

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Bengt Richter
On Mon, 14 Mar 2005 23:44:46 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: >Ron Garret wrote: >> What I'm really trying to do is to create enumerated types such that if: >> >> e1 = enum(lst) and v = e1(x) >> >> then >> >> (x in lst) and (e1[v] == x) > >Use a class with __call__ and __getitem

Re: __getitem__ method on (meta)classes

2005-03-15 Thread Ron Garret
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Bengt Richter) wrote: > >Did you mean type(x).__getitem__(x,y)? > > > Not if x is a classmethod, Oh yeah, right. Duh! > >And where is this documented? > Between the lines in my previous post ;-) I see. I guess I wasn't asking a stupid questi

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Steven Bethard
Ron Garret wrote: And the code I ended up with is: # Inheriting from type, not object, is the key: class enum_metaclass(type): def __getitem__(self, index): return self.vals[index] def enum(vals): class enum(object): __metaclass__ = enum_metaclass def __init__(self, val): try:

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Ron Garret
In article <[EMAIL PROTECTED]>, Ron Garret <[EMAIL PROTECTED]> wrote: Wow, this is really cool: > What I'm really trying to do is to create enumerated types... And the code I ended up with is: # Inheriting from type, not object, is the key: class enum_metaclass(type): def __getitem__(self, i

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Bengt Richter
On Mon, 14 Mar 2005 22:00:38 -0800, Ron Garret <[EMAIL PROTECTED]> wrote: >In article <[EMAIL PROTECTED]>, > Leif K-Brooks <[EMAIL PROTECTED]> wrote: > >> [EMAIL PROTECTED] wrote: >> > Why doesn't this work? >> > >> > >> def foo(lst): >> > >> > ... class baz(object): >> > ... def __ge

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Steven Bethard
Ron Garret wrote: What I'm really trying to do is to create enumerated types such that if: e1 = enum(lst) and v = e1(x) then (x in lst) and (e1[v] == x) Use a class with __call__ and __getitem__: py> class enum(object): ... def __init__(self, vals): ... self.vals = vals ... def __ca

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Ron Garret
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Bengt Richter) wrote: > On 14 Mar 2005 17:43:53 -0800, [EMAIL PROTECTED] wrote: > > > > >Why doesn't this work? > > > def foo(lst): > >... class baz(object): > >... def __getitem__(cls, idx): return cls.lst[idx] > >... __getitem__

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Ron Garret
In article <[EMAIL PROTECTED]>, Leif K-Brooks <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Why doesn't this work? > > > > > def foo(lst): > > > > ... class baz(object): > > ... def __getitem__(cls, idx): return cls.lst[idx] > > ... __getitem__=classmethod(__getitem__

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Michele Simionato
Leif Brooks: >with new-style classes, x[y] and type(x).__getitem__(y) are >synonymous. Yes, but check the discussion around SF789262. The call ``x[y]`` is converted to ``type(x).__getitem__(x,y)`` *only if* ``__getitem__`` is explicitely defined in ``type(x)``. If ``type(x)`` does not define ``__

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: Why doesn't this work? def foo(lst): ... class baz(object): ... def __getitem__(cls, idx): return cls.lst[idx] ... __getitem__=classmethod(__getitem__) ... baz.lst = lst ... return baz ... I thought x[y] and x.__getitem__(y) were supposed to always be synonym

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Bengt Richter
On 14 Mar 2005 17:43:53 -0800, [EMAIL PROTECTED] wrote: > >Why doesn't this work? > def foo(lst): >... class baz(object): >... def __getitem__(cls, idx): return cls.lst[idx] >... __getitem__=classmethod(__getitem__) >... baz.lst = lst >... return baz >... f = foo([1,2,3]) >

Re: __getitem__ method on (meta)classes

2005-03-14 Thread Simon Percivall
Well, they're not synonymous. At least not in that context. If you haven't already tried it, what you're doing will fail for instances as well. Look in typeobject.c to see why. The gist of it is that the special methods are looked up on the type rather than the instance (on the metaclass rather tha

__getitem__ method on (meta)classes

2005-03-14 Thread ron
Why doesn't this work? >>> def foo(lst): ... class baz(object): ... def __getitem__(cls, idx): return cls.lst[idx] ... __getitem__=classmethod(__getitem__) ... baz.lst = lst ... return baz ... >>> f = foo([1,2,3]) >>> f[0] Traceback (most recent call last): File "", line 1, in ? T