473,703 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__getitem__ method on (meta)classes

ron

Why doesn't this work?
def foo(lst): .... class baz(object):
.... def __getitem__(cls , idx): return cls.lst[idx]
.... __getitem__=cla ssmethod(__geti tem__)
.... baz.lst = lst
.... return baz
.... f = foo([1,2,3])
f[0] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object f.__getitem__(0 ) 1

I thought x[y] and x.__getitem__(y ) were supposed to always be
synonymous.

Thanks,
rg

Jul 18 '05 #1
21 3572
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 than on the class, in your case), so the internal
lookup mechanism ignore instance attributes completely in this case.

Jul 18 '05 #2
On 14 Mar 2005 17:43:53 -0800, ro*@flownet.com wrote:

Why doesn't this work?
def foo(lst):... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__=cla ssmethod(__geti tem__)
... baz.lst = lst
... return baz
... f = foo([1,2,3])
f[0]Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object f.__getitem__(0 )1

I thought x[y] and x.__getitem__(y ) were supposed to always be
synonymous.

Yes, but what was your "x"?
Note:
def foo(lst): ... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__ = classmethod(__g etitem__)
... baz.lst = lst
... return baz
... f = foo([1,2,3])
f <class '__main__.baz'>

Your "x" was the baz *class*, and the baz *class* is not subscriptable *itself*,
even though it defines subscripting for its instances.
To be ordinarily subscriptable, type(f).__getit em__ would have to succeed, which it doesn't:
type(f) <type 'type'> type(f).__getit em__ Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object 'type' has no attribute '__getitem__'

However, a baz instance is different:
finst = f()
finst <__main__.baz object at 0x02EF168C>

Its type does have a __getitem__ attribute:
type(finst).__g etitem__ <bound method type.__getitem_ _ of <class '__main__.baz'> >

I think "bound method" is a bit of a misnomer for a classmethod, though
it is a method, and it is bound, just to the class instead of the instance.
(see below)
type(finst).__g etitem__(0) 1

Or straightforward indexing syntax:
finst[0], finst[1], finst[2] (1, 2, 3)

Contrast with the way an ordinary method repr's when you access it
via class and instance:
class C(object): ... def ordinary_method (self): return 'Hi'
... c=C()
C.ordinary_meth od <unbound method C.ordinary_meth od> c.ordinary_meth od <bound method C.ordinary_meth od of <__main__.C object at 0x02EF164C>> c.ordinary_meth od() 'Hi' C.ordinary_meth od <unbound method C.ordinary_meth od> C.ordinary_meth od(c) 'Hi'

Note the difference between ordinary and classmethod:
type(c).ordinar y_method <unbound method C.ordinary_meth od> type(finst).__g etitem__

<bound method type.__getitem_ _ of <class '__main__.baz'> >

BTW, I see a class factory, but no "(meta)"cla ss per se.

Regards,
Bengt Richter
Jul 18 '05 #3
ro*@flownet.com wrote:
Why doesn't this work?

def foo(lst):
... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__=cla ssmethod(__geti tem__)
... baz.lst = lst
... return baz
...

I thought x[y] and x.__getitem__(y ) were supposed to always be
synonymous.


No, with new-style classes, x[y] and type(x).__getit em__(y) are
synonymous. This works:
def foo(lst): .... class bar(type):
.... def __getitem__(sel f, index):
.... return self.lst[index]
.... class baz(object):
.... __metaclass__ = bar
.... baz.lst = lst
.... return baz
.... foo([1,2,3])[0]

1
Jul 18 '05 #4
Leif Brooks:
with new-style classes, x[y] and type(x).__getit em__(y) are
synonymous.


Yes, but check the discussion around SF789262. The call ``x[y]`` is
converted to ``type(x).__get item__(x,y)``
*only if* ``__getitem__`` is explicitely defined in ``type(x)``. If
``type(x)`` does not define ``__getitem__`` directly, but only
indirectly via delegation (i.e. overriding ``__getattribut e__``),
then the second form (i.e. ``type(x).__get item__(x,y)``) works but
not the first one (i.e. ``x[y]`` raises an error).

Michele Simionato

Jul 18 '05 #5
In article <39************ *@individual.ne t>,
Leif K-Brooks <eu*****@ecritt ers.biz> wrote:
ro*@flownet.com wrote:
Why doesn't this work?

>def foo(lst):


... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__=cla ssmethod(__geti tem__)
... baz.lst = lst
... return baz
...

I thought x[y] and x.__getitem__(y ) were supposed to always be
synonymous.


No, with new-style classes, x[y] and type(x).__getit em__(y) are
synonymous.


Ah.

Did you mean type(x).__getit em__(x,y)?

And where is this documented?

rg
Jul 18 '05 #6
In article <42************ ****@news.oz.ne t>,
bo**@oz.net (Bengt Richter) wrote:
On 14 Mar 2005 17:43:53 -0800, ro*@flownet.com wrote:

Why doesn't this work?
> def foo(lst):... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__=cla ssmethod(__geti tem__)
... baz.lst = lst
... return baz
...
> f = foo([1,2,3])
> f[0]

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
> f.__getitem__(0 )

1
>

I thought x[y] and x.__getitem__(y ) were supposed to always be
synonymous.

Yes, but what was your "x"?
Note:
>>> def foo(lst): ... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__ = classmethod(__g etitem__)
... baz.lst = lst
... return baz
... >>> f = foo([1,2,3])
>>> f

<class '__main__.baz'>

Your "x" was the baz *class*, and the baz *class* is not subscriptable
*itself*,


But why not? __getitem__ is a class method, not an instance method.

(You can treat that as a rhetorical question. I know the answer already
from other postings in this thread.)
BTW, I see a class factory, but no "(meta)"cla ss per se.


That's because I took out extraneous code to not distract from the
problem I was having, and in the process took out all the useful stuff
:-)

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)

In other words, I want to map values onto their representations using
the () operator, and representations onto their values using the []
operator. That requires me to define the [] operator on the enumerated
classes themselves. So my actual code was:

def enum(vals):
class enum(object):
def __init__(self, val):
try:
self.val = type(self).vals .index(val)
except:
raise TypeError, "%s is not a valid %s" % (val, type(self))
def __getitem__(sel f, index): return self.vals.index (index)
__getitem__=cla ssmethod(__geti tem__)
enum.vals = vals
return enum
(Actually, what I'm really trying to do is create a whole hierarchy of
static type descriptors and automatically generate database schema from
those descriptors, but that's a story for another day.)

Thanks for all the responses.

rg
Jul 18 '05 #7
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 __call__(self, val):
.... return self.vals.index (val)
.... def __getitem__(sel f, index):
.... return self.vals[index]
....
py> lst = 'abcd'
py> x = 'b'
py> e1 = enum(lst)
py> v = e1(x)
py> (x in lst) and (e1[v] == x)
True

STeVe
Jul 18 '05 #8
On Mon, 14 Mar 2005 22:00:38 -0800, Ron Garret <rN*******@flow net.com> wrote:
In article <39************ *@individual.ne t>,
Leif K-Brooks <eu*****@ecritt ers.biz> wrote:
ro*@flownet.com wrote:
> Why doesn't this work?
>
>
>>>>def foo(lst):
>
> ... class baz(object):
> ... def __getitem__(cls , idx): return cls.lst[idx]
> ... __getitem__=cla ssmethod(__geti tem__)
> ... baz.lst = lst
> ... return baz
> ...
>
> I thought x[y] and x.__getitem__(y ) were supposed to always be
> synonymous.
No, with new-style classes, x[y] and type(x).__getit em__(y) are
synonymous.


Ah.

Did you mean type(x).__getit em__(x,y)?

Not if x is a classmethod, since type(x).__getit em__ gets you a bound-to-the-class method
instead of the usual unbound method, which would want the x instance as the first argument.
def foo(lst): ... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... __getitem__=cla ssmethod(__geti tem__)
... baz.lst = lst
... return baz
... f = foo([1,2,3])()
type(f).__getit em__ <bound method type.__getitem_ _ of <class '__main__.baz'> > type(f).__getit em__(0) 1

Leaving out the classmethod:
def foo(lst): ... class baz(object):
... def __getitem__(cls , idx): return cls.lst[idx]
... baz.lst = lst
... return baz
... f = foo([1,2,3])()
type(f).__getit em__ <unbound method baz.__getitem__ > type(f).__getit em__(f, 0)

1
And where is this documented?

Between the lines in my previous post ;-)
Regards,
Bengt Richter
Jul 18 '05 #9
In article <rN************ *************** **@news.gha.cha rtermi.net>,
Ron Garret <rN*******@flow net.com> 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__(sel f, index):
return self.vals[index]

def enum(vals):
class enum(object):
__metaclass__ = enum_metaclass
def __init__(self, val):
try:
self.val = type(self).vals .index(val)
except:
raise TypeError, "%s is not a valid %s" % (val, type(self))
enum.vals = vals
return enum
So now I can do, e.g.:
e1 = enum(['a','b','c'])
e1('a') <__main__.enu m object at 0x393c50> e1('x') Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 8, in __init__
TypeError: x is not a valid <class '__main__.enum' >
But I can also do this:
for x in e1: print x ....
a
b
c


Note that there's no __iter__ method anywhere! It makes an interesting
little puzzle to figure out why this works. (It totally blew me away
when I first tried it. Took me about five minutes of head scratching to
figure it out.)

rg
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
12621
by: Average_Joe | last post by:
Hello PHP people, Was wondering if PHP5 had some sort of "nested class" functionality. I'm trying to figure out how to return an object (with a private constructor) that has access to variables in another class. Something like: $obj = $factory->getObject("1234");
9
1907
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in the PEP 218. I don't understand why the sets has an interface of mapping object without associated value and not an interface of sequence ? For me, sets are first sequences. I use set where lists are inefficient or inadapted, for example when I...
1
1981
by: simon | last post by:
What i'm trying to do is tie special methods of a "proxy" instance to another instance: def test1(): class Container: def __init__( self, data ): self.data = data self.__getitem__ = self.data.__getitem__ data = range(10)
4
2079
by: Tuure Laurinolli | last post by:
Someone pasted the original version of the following code snippet on #python today. I started investigating why the new-style class didn't work as expected, and found that at least some instances of new-style classes apparently don't return true for PyInstance_Check, which causes a problem in PySequence_Check, since it will only do an attribute lookup for instances. Things probably shouldn't be this way. Should I go to python-dev with...
13
1939
by: Thomas Heller | last post by:
I'm trying to implement __iter__ on an abstract base class while I don't know whether subclasses support that or not. Hope that makes sense, if not, this code should be clearer: class Base: def __getattr__(self, name): if name == "__iter__" and hasattr(self, "Iterator"): return self.Iterator raise AttributeError, name
13
1475
by: Jeremy Sanders | last post by:
Is it possible to implement some sort of "lazy" creation of objects only when the object is used, but behaving in the same way as the object? For instance: class Foo: def __init__(self, val): """This is really slow.""" self.num = val
24
10506
by: Chad Z. Hower aka Kudzu | last post by:
I need to store a list of classes, not the instances of the classes. I had planned to use an array, but can use a collection or some other form if more appropriate. The problem I am having is that I cannot find the C# syntax I need to reference a class type. public <class type> PowerTour = new <class type> {form1, form2}; Then later I will access this to get a list of classes which I will then
5
1504
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create an instance of an Old-Style class without defining a __contains__ but instead define a __getitem__ method in which we raise KeyError. Next we repeatedly use the 'in' operator to test to see whether something, a string, an int, etc is an attribute...
0
1055
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bourdenas@gmail.com> escribi�: In a very strict sense, I'd say that all those references to "method decorators" are wrong - because those "def" statements inside a "class" statement define functions, not methods. In that sense almost every Python programmer is wrong too: in this example class X:
0
8654
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8941
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5910
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2037
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.