简体   繁体   English

在类调用中设置类属性

[英]setting class attributes in class call

I am just wondering if there is a shortcut to setting default attributes. 我只是想知道是否有设置默认属性的捷径。 I generally would do 我通常会做

class class1(object):
    def __init__(self, att1='X'):
         self.att1 = att1

Is there a shortcut in the lines off 线下有捷径吗

class class1(object):
    def __init__(self, self.att1='X'):

I assume at the moment of the call, the object does not exist, so seem logical that this way does not work, but maybe there is sort of less verbose way to deal with this when there is a lot more attributes to be set. 我假设在调用的那一刻,该对象不存在,似乎很合逻辑,以至于这种方式行不通,但是当要设置更多的属性时,也许有一种不太冗长的方式来处理该对象。 Any ideas? 有任何想法吗?

Try something like: 尝试类似:

class class1(object):
    att1='X'
    def __init__(self, **kwargs):
         self.__dict__.update(kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM