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
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
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
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(
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) ==
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,
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
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)
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 __
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
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
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:
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
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
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
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__
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__
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 ``__
[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
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])
>
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
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
22 matches
Mail list logo