(meta)类的__getitem__方法 [英] __getitem__ method on (meta)classes

查看:81
本文介绍了(meta)类的__getitem__方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么这不起作用?


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

....返回baz

.... f = foo([1,2,3])
f [0]
回溯(最近一次呼叫最后一次):

文件"< stdin>",第1行,在?

TypeError:unsubscriptable对象f .__ getitem __(0)
1
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 "<stdin>", line 1, in ?
TypeError: unsubscriptable object f.__getitem__(0) 1



我以为x [y]和x .__ getitem __(y)应该总是

同义词。


谢谢,

rg


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

Thanks,
rg

推荐答案

好吧,他们不是同义词。至少不是在那种情况下。如果你还没有试过它,那么你所做的事情就会失败,因为

很好。查看typeobject.c以了解原因。它的要点是

特殊方法是在类型而不是实例上查找的(在你的情况下,在元类而不是在类上的
),所以内部

查找机制在这种情况下完全忽略实例属性。

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.


2005年3月14日17:43:53 - 0800, ro *@flownet.com 写道:
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):返回cls.lst [idx]
... __getitem __ = classmethod(__ getitem__)
... baz.lst = lst
...返回baz
.. .f = foo([1,2,3])
f [0] Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,in?
TypeError:unsubscriptable对象f .__ getitem __(0)1

我以为x [y]和x .__ getitem __(y)应该总是
同义。
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 "<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.



是的,但你的x是什么?

注意:


Yes, but what was your "x"?
Note:

def foo( lst):
... class baz(object):

... def __getitem __(cls,idx):return cls.lst [idx]

... __getitem__ = classmethod(__ getitem__)

... baz.lst = lst

...返回baz

... f = foo([1,2,3])
f
< class''__ main __。baz''>


你的x是baz * class *,而baz * class *不是可订阅的*本身*,

即使它定义了其实例的下标。

通常可以订阅,类型(f).__ getitem__必须成功,它不会:

类型(f)
< type''type''>类型(f).__ getitem__
回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

AttributeError :type object''type''没有属性''__ getitem__''


但是,一个baz实例是不同的:

finst = f()
finst
< __ main __。baz对象在0x02EF168C>


它的类型确实有__getitem__属性:

type(finst)。 __getitem__
<绑定方法类型.__ getitem__ of< class''_ _ main __。baz''>>


我认为绑定方法对于一个类方法来说有点用词不当,不过它是一个方法,它只是对类而不是实例绑定。

(见下文)

类型(finst).__ getitem __(0)
1


或简单的索引语法:

finst [0 ],finst [1],finst [2]
(1,2,3)


对比普通方法在你访问它时的方式br />
通过类和实例:

class C(object):
... def ordinary_method(self):return''嗨''

... c = C()
C.ordinary_method
< unbound method C.ordinary_method> c.ordinary_method
<绑定方法C.ordinary_method of< __ main __。C对象位于0x02EF164C>> c.ordinary_method()
''嗨''C.ordinary_method
< unbound method C.ordinary_method> C.ordinary_method(c)
''嗨''


注意普通方法和类方法之间的区别:

type(c).ordinary_method
< unbound method C.ordinary_method> type(finst).__ getitem __
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 <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).__getitem__ would have to succeed, which it doesn''t:
type(f) <type ''type''> type(f).__getitem__ 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).__getitem__ <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).__getitem__(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_method <unbound method C.ordinary_method> c.ordinary_method <bound method C.ordinary_method of <__main__.C object at 0x02EF164C>> c.ordinary_method() ''Hi'' C.ordinary_method <unbound method C.ordinary_method> C.ordinary_method(c) ''Hi''

Note the difference between ordinary and classmethod:
type(c).ordinary_method <unbound method C.ordinary_method> type(finst).__getitem__



< bound method type .__ getitem__ of< class''_ _ main __。baz''>>


BTW,我看到一个班级工厂,但没有(meta)类本身。


问候,

Bengt Richter


<bound method type.__getitem__ of <class ''__main__.baz''>>

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

Regards,
Bengt Richter


ro *@flownet.com 写道:
为什么这不起作用?

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
...返回baz
...

我认为x [y]和x .__ getitem __(y)应该永远是
的同义词。
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
synonymous.




不,新风格的类,x [y]和类型(x).__ getitem __(y)是

同义词。这有效:



No, with new-style classes, x[y] and type(x).__getitem__(y) are
synonymous. This works:

def foo(lst):
.... class bar(type):

.... def __getitem __(自我,索引):

....返回self.lst [index]

.... class baz(对象):

.... __metaclass__ = bar

.... baz.lst = lst

....返回baz

.... foo([1,2,3])[0]
def foo(lst): .... class bar(type):
.... def __getitem__(self, index):
.... return self.lst[index]
.... class baz(object):
.... __metaclass__ = bar
.... baz.lst = lst
.... return baz
.... foo([1,2,3])[0]



1


1


这篇关于(meta)类的__getitem__方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆