python【自写】命名元组

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Yellow_python/article/details/100776883

内置方法

from collections import namedtuple
free_falling_body = namedtuple('free_falling_body', ['g', 't'])
h = free_falling_body(9.8, 2 ** (1 / 2))
print(h)  # 自由落体运动
print(h.g * h.t ** 2 / 2)  # 自由落体高度

free_falling_body(g=9.8, t=1.4142135623730951)
9.800000000000002

自创

class Tuple:
    t = ('a', 'b', 'c')
    for i in t:
        exec("%s='%s'" % (i, i))

print(' '.join(Tuple.t))
print(Tuple.b, Tuple.i)
print(Tuple.__dict__)

打印结果

a b c
b c
{'__module__': '__main__', 't': ('a', 'b', 'c'), 'i': 'c', 'a': 'a', 'b': 'b', 'c': 'c', '__dict__': <attribute '__dict__' of 'Tuple' objects>, '__weakref__': <attribute '__weakref__' of 'Tuple' objects>, '__doc__': None}

猜你喜欢

转载自blog.csdn.net/Yellow_python/article/details/100776883