用namedtuple时相当于新建了一个类,如以下代码,建立 了一个名叫Poooi的类,这个类有一个名叫x和名叫y的私有变量
namedtuple('类名',['x','y'])
from collections import namedtuple
Poi = namedtuple('Pooooi',['x','y'])
p = Poi(5,6)
print(Poi)
print(p)
print(p.x)
print(p.y)
输出
<class '__main__.Pooooi'>
Pooooi(x=5, y=6)
5
6
等价于
class Poooi:
def __init__(self,x,y):
self.x = x
self.y = y
p = Poooi(5,6)
print(Poooi)
print(p)
print(p.x)
print(p.y)
输出
<class '__main__.Poooi'>
<__main__.Poooi object at 0x000001C139AF4BE0>
5
6