在Python中使用属性的对象创建简洁
我想将带有属性键
设置为42
的对象传递到功能f
。这将仅完成一次,并且对象及其类型被丢弃。这是我的解决方案:
from collections import namedtuple
def f(a):
print(a.key)
f(namedtuple('T', ['key'])(42))
是否有比nequ tameTuple('t',['key'])(42)
少的方法。
I want to pass an object with attribute key
set to 42
to function f
. This will be done only once, and the object and its type is discarded. Here is my solution:
from collections import namedtuple
def f(a):
print(a.key)
f(namedtuple('T', ['key'])(42))
Is there a less cumbersome way than namedtuple('T', ['key'])(42)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论