我应该如何在Python中声明实例变量的默认值? [英] How should I declare default values for instance variables in Python?

查看:145
本文介绍了我应该如何在Python中声明实例变量的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该给我的类成员默认值如下:

  class Foo:
num = 1

还是这样?

 code> class Foo:
def __init __(self):
self.num = 1


b $ b

此问题中,我发现在这两种情况下,

  bar = Foo()
bar.num + = 1



我知道第一种方法会给我一个类变量,而第二个不会。但是,如果我不需要一个类变量,但只需要为我的实例变量设置一个默认值,这两种方法同样好吗?



我注意到的一件事是,在Django教程中,他们使用第二种方法来声明模型。我个人认为第二种方法更优雅,

解决方案

扩展bp的答案,我想告诉你他的意思是不可变的



首先,这是好的:

 > ;> class TestB():
... def __init __(self,attr = 1):
... self.attr = attr
...
>> ; a = TestB()
>>>> b = TestB()
>>>> a.attr = 2
>>>> a.attr
2
>>>> b.attr
1

但是,这仅适用于不可变如果默认值是可变的(意味着它可以被替换),则会发生这种情况:

 > class Test():
... def __init __(self,attr = []):
... self.attr = attr
...
> > a = Test()
>>>> b = Test()
>>>> a.attr.append(1)
>>> a.attr
[1]
>>>> b.attr
[1]
>>>>

注意a和b都有一个共享属性。这通常是不需要的。



这是Pythonic为实例变量定义默认值的方法,当类型是可变的:

 >>> class TestC():
... def __init __(self,attr = None):
...如果attr为None:
... attr = []
。 .. self.attr = attr
...
>>>> a = TestC()
>>>> b = TestC()
>>>> a.attr.append(1)
>>> a.attr
[1]
>>>> b.attr
[]

我的第一个代码段工作的原因是,不可变类型,Python创建一个新的实例,当你想要一个。如果你需要添加1到1,Python为你做一个新的2,因为旧1不能更改。原因主要是哈希,我相信。


Should I give my class members default values like this:

class Foo:
    num = 1

or like this?

class Foo:
    def __init__(self):
        self.num = 1

In this question I discovered that in both cases,

bar = Foo()
bar.num += 1

is a well-defined operation.

I understand that the first method will give me a class variable while the second one will not. However, if I do not require a class variable, but only need to set a default value for my instance variables, are both methods equally good? Or one of them more 'pythonic' than the other?

One thing I've noticed is that in the Django tutorial, they use the second method to declare Models. Personally I think the second method is more elegant, but I'd like to know what the 'standard' way is.

解决方案

Extending bp's answer, I wanted to show you what he meant by immutable types.

First, this is okay:

>>> class TestB():
...     def __init__(self, attr=1):
...         self.attr = attr
...     
>>> a = TestB()
>>> b = TestB()
>>> a.attr = 2
>>> a.attr
2
>>> b.attr
1

However, this only works for immutable (unchangable) types. If the default value was mutable (meaning it can be replaced), this would happen instead:

>>> class Test():
...     def __init__(self, attr=[]):
...         self.attr = attr
...     
>>> a = Test()
>>> b = Test()
>>> a.attr.append(1)
>>> a.attr
[1]
>>> b.attr
[1]
>>> 

Note both a and b have a shared attribute. This is often unwanted.

This is the Pythonic way of defining default values for instance variables, when the type is mutable:

>>> class TestC():
...     def __init__(self, attr=None):
...         if attr is None:
...             attr = []
...         self.attr = attr
...     
>>> a = TestC()
>>> b = TestC()
>>> a.attr.append(1)
>>> a.attr
[1]
>>> b.attr
[]

The reason my first snippet of code works is because, with immutable types, Python creates a new instance of it whenever you want one. If you needed to add 1 to 1, Python makes a new 2 for you, because the old 1 cannot be changed. The reason is mostly for hashing, I believe.

这篇关于我应该如何在Python中声明实例变量的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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