我在学蟒蛇。当谈到官方库中的集合模块时,我发现了NamedTuple的代码片段,如下所示:
for i, name in enumerate(field_names):
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)这是NamedTuple生成的代码的一部分。生成的代码如下:
name = property(itemgetter(0), doc='Alias for field number 0')
age = property(itemgetter(1), doc='Alias for field number 1')我的问题是:
Itemgetter(0)是一个需要对象作为参数的函数。但是属性不会向itemgetter传递任何参数。那么这是如何工作的呢?
谢谢!
这是使用属性的整个代码:
class Person(tuple):
'Person(name, age)'
__slots__ = ()
_fields = ('name', 'age')
def __new__(_cls, name, age):
'Create new instance of Person(name, age)'
print sys._getframe().f_code.co_name
return _tuple.__new__(_cls, (name, age))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new Person object from a sequence or iterable'
print sys._getframe().f_code.co_name
result = new(cls, iterable)
if len(result) != 2:
raise TypeError('Expected 2 arguments, got %d' % len(result))
return result
def __repr__(self):
'Return a nicely formatted representation string'
print sys._getframe().f_code.co_name
return 'Person(name=%r, age=%r)' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
print sys._getframe().f_code.co_name
return OrderedDict(zip(self._fields, self))
def _replace(_self, **kwds):
'Return a new Person object replacing specified fields with new values'
print sys._getframe().f_code.co_name
result = _self._make(map(kwds.pop, ('name', 'age'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % kwds.keys())
return result
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
print sys._getframe().f_code.co_name
return tuple(self)
name = property(itemgetter(0), doc='Alias for field number 0')
age = property(itemgetter(1), doc='Alias for field number 1')发布于 2013-09-18 23:12:31
itemgetter不是一个函数,它是一个类,它的实例是可调用的(参见FineManual)。property实例将使用当前对象作为参数来调用它(这就是属性的用途)。
让我们总结一下。假设是这样:
point = tuple(1, 2)
getx = itemgetter(0)将point传递给getx()将返回point[0] (实际上是point.__getitem__[0],其中point[0]是语法糖)
现在,如果我们继承tuple子类并添加一个property
class Point(tuple):
@property
def x(self):
return self[0]@装饰器语法实际上是以下语法的糖:
class Point(tuple):
def x(self):
return self[0]
x = property(fget=x)因此,函数x成为property实例的fget属性,并且类语句的名称空间中的名称x被重新绑定到这个property实例。
现在,让我们创建一个Point实例:
point = Point(1, 2)然后,在计算point.x时,属性查找规则将在Point上(实际上在point.__class__上)找到"x“property对象,注意它有一个__get__()方法,并且根据描述符协议将返回Point.x.__get__(point, Point.__class__)的结果。由于property.__get__(obj, cls)主要实现为return self.fget(obj),因此这将返回使用point调用的x函数的结果作为self参数。IOW:
point.x等同于
Point.x.__get__(point, point.__class__)这相当于
Point.x.fget(point)它等同于(NB :这里'x‘指的是作为fget参数传递给property的x函数,而不是Point.x
x(point)这相当于
point[0]由于itemgetter(0)(point)等同于point[0],因此可以看到x = property(itemgetter(0))是如何工作的。
https://stackoverflow.com/questions/18875170
复制相似问题