使用新样式类型键入模拟问题 [英] Type emulation issues with new style classes

查看:64
本文介绍了使用新样式类型键入模拟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Z级的行为与Y级相同?


Chris


Y级:

值= 42

def __hasattr __(自我,姓名):

如果名字==''__int__'':

返回True

def __getattr __(self,name):

如果name ==''__ int__'':

返回lambda:self。价值


等级Z(物件):

值= 42

def __hasattr __(自我,姓名):

如果名称==''__ int__'':

返回True

def __getattr __(自我,姓名):

如果名称== '' __int__ '':

返回拉姆达:self.value
y,z = Y(),Z()
print int(y)
42 print int(z)



TypeError:int()参数必须是字符串或数字

解决方案

类Z(对象):

value = 42

def __hasattr __(self,name):

if name ==''__ int__'':

返回True

def __int __(自我):

返回self.value


嗯,也许我不明白你需要什么。


-

你不知道为什么你的Python应用程序崩溃了吗?

看看 http://www.pycrash.org

-

通过Mailgate.ORG服务器发布 - http://www.Mailgate.ORG


我需要Python调用数字,列表,

等的仿真方法,当这些方法通过__getattr__提供而不是定义时
实例或类__dict__中的
。我一直在PEP 252上下试图确定
如何确定仿真方法的存在与否

的新风格类,但我不是祝你好运。


" Carmine Noviello"写道:类Z(对象):
值= 42
DEF __hasattr __(个体,名称):
如果名称== '' __int __ '':
返回True
def __int __(自我):
返回self.value

嗯,也许我不明白你需要什么。




我需要知道我如何能够做到我之前用旧式的演示来演绎
类,这是Python的内省机制的动态链接/>
提供一种__int__方法(或任选__str__,__lt__,__le__,__eq__

,__ ne__,__gt__,__ge__,__len__,__getitem__,__add__,__sub__,__mul__,

__floordiv__,__mod__,__divmod__,__pow__,__lshift__,...广告nausuem)。

旧风格的班会,让我做到这一点,但我不能确定如何做到这一点

新款式。


Chris


文章< C0 ********* *******@nwrdny03.gnilink .net>,

Chris< fe ************* @ spamgourmet.com>写道:
类Z(对象):
值= 42
DEF __hasattr __(个体,名称):
如果名称== '' __int__ '':
返回True
DEF __getattr __(个体,名称):
如果名称== '' __int__ '':
返回拉姆达:self.value



IIRC,__ getattribute__可以做你想做的事,但我没有时间来支票
查看。

-

Aahz(aa**@pythoncraft.com)< *> http://www.pythoncraft.com/


不要嘲笑循环的乐趣。不要更改正在循环的列表。

--Remco Gerlich,comp.lang.python


Is there any way to make the class Z behave the same way as class Y?

Chris

class Y:
value = 42
def __hasattr__(self, name):
if name == ''__int__'':
return True
def __getattr__(self, name):
if name == ''__int__'':
return lambda: self.value

class Z(object):
value = 42
def __hasattr__(self, name):
if name == ''__int__'':
return True
def __getattr__(self, name):
if name == ''__int__'':
return lambda: self.value

y, z = Y(), Z()
print int(y) 42 print int(z)


TypeError: int() argument must be a string or a number

解决方案

class Z(object):
value = 42
def __hasattr__(self, name):
if name == ''__int__'':
return True
def __int__(self):
return self.value

Umm, maybe I haven''t understood what you need.

--
Don''t you know why your Python application has crashed?
Take a look to http://www.pycrash.org
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG


I need to have Python call the the emulation methods for numbers, lists,
etc, when those methods are provided though __getattr__ rather than defined
in the instance or class __dict__. I''ve been up and down PEP 252 trying to
determine how Python determines the presence or absence of emulation methods
for new style classes, but I''m not having any luck.

"Carmine Noviello" wrote:

class Z(object):
value = 42
def __hasattr__(self, name):
if name == ''__int__'':
return True
def __int__(self):
return self.value
Umm, maybe I haven''t understood what you need.



I need to know how I can do what I previously demonstrated with an old style
class, that is hook into Python''s introspection mechanism to dynamically
provide an __int__ method (or optionally __str__, __lt__, __le__, __eq__
,__ne__, __gt__, __ge__, __len__, __getitem__, __add__, __sub__, __mul__,
__floordiv__, __mod__, __divmod__, __pow__, __lshift__, ... ad nausuem).
Old style classes will let me do this, but I cannot determine how to do this
with new style classes.

Chris


In article <C0****************@nwrdny03.gnilink.net>,
Chris <fe*************@spamgourmet.com> wrote:


class Z(object):
value = 42
def __hasattr__(self, name):
if name == ''__int__'':
return True
def __getattr__(self, name):
if name == ''__int__'':
return lambda: self.value



IIRC, __getattribute__ may do what you want, but I don''t have time to
check.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python


这篇关于使用新样式类型键入模拟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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