0
나는 다음과 같은 파이썬 코드를 가지고 있지만 파이썬에서는 잘 작동하지만 IronPython 2.6에서는 다음과 같은 오류가 발생합니다. 그 이유는 무엇입니까?IronPython에서 자동 생성이 실패한 것은 Python에서 작동합니까?
======================================================================
ERROR: testAutoProp (__main__.testProperty)
----------------------------------------------------------------------
Traceback (most recent call last):
File "oproperty.py", line 66, in testAutoProp
a.x = 200
File "oproperty.py", line 31, in __set__
getattr(obj, self.fset.__name__)(value)
AttributeError: 'A' object has no attribute '<lambda$48>'
----------------------------------------------------------------------
Ran 1 test in 0.234s
FAILED (errors=1)
는 여기서, IronPython의에 람다는 <lambda$48>
대신 단지 <lambda>
같은 이름을 가지고 있지만 나에게 보이는 그냥 코드를 역 추적보고 코드
#provides an overridable version of the property keyword
import unittest
#provides an overridable version of the property keyword
class OProperty(object):
"""Based on the emulation of PyProperty_Type() in Objects/descrobject.c"""
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
if self.fget.__name__ == '<lambda>' or not self.fget.__name__:
return self.fget(obj)
else:
return getattr(obj, self.fget.__name__)()
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
self.fset(obj, value)
else:
getattr(obj, self.fset.__name__)(value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
if self.fdel.__name__ == '<lambda>' or not self.fdel.__name__:
self.fdel(obj)
else:
getattr(obj, self.fdel.__name__)()
def autoProperty(attrname, desc):
"Try to auto gen getters and setting for any property type"
getFn = lambda self: getattr(self, attrname)
setFn = lambda self, v: setattr(self, attrname, v)
#set the corresponding function in the descriptor
desc.fget = getFn
desc.fset = setFn
#if there is a blank colname let X property sort it out
#if hasattr(desc, "colname") and desc.colname == "":
# i = 0
# while attrname[ i ] == "_":
# i = i + 1
# desc.colname = attrname[i:]
return desc
class testProperty(unittest.TestCase):
def testAutoProp(self):
class A(object):
def __init__(self):
self._x = 50
x = autoProperty("_x", OProperty())
a = A()
a.x = 200
if __name__ == "__main__":
unittest.main()
Duncan이 완벽하게 이해했으며, 코드를 수정하면 테스트가 바로 작동합니다. 건배. 존. – jonmiddleton