Python中的对象初始值设定项语法(C#)? [英] Object initializer syntax (c#) in python?

查看:45
本文介绍了Python中的对象初始值设定项语法(C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种快速的方法来初始化python中的对象.

I was wondering if there is a quick way to initialise an object in python.

例如,在c#中,您可以实例化对象并设置字段/属性,例如...

For example in c# you can instantiate an object and set the fields/properties like...

SomeClass myObject = new SomeClass() { variableX = "value", variableY = 120 };

谢谢

布莱恩

推荐答案

如果您想要带有某些字段的快速脏对象,我强烈建议使用

If you want a quick dirty object with some fields, I highly suggest using namedtuples

from collections import namedtuple
SomeClass = namedtuple('Name of class', ['variableX', 'variableY'], verbose=True)
myObject = SomeClass("value", 120)

print myObject.variableX

这篇关于Python中的对象初始值设定项语法(C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆