Python - 在类中声明属性的约定是什么?
在 Python 中,我可以在整个类中声明属性。例如:
class Foo:
def __init__(self):
self.a = 0
def foo(self):
self.b = 0
当我有一个包含很多属性的大类时,很难检索类中的所有属性。
是下面的代码(a)更好还是下面的代码(b)更好:
a)这里,很难找到所有属性:
class Foo:
def __init__(self):
foo_1()
foo_2()
def foo_1(self):
self.a = 0
self.b = 0
def foo_2(self):
self.c = 0
b)这里,它是很容易找到所有属性,但它漂亮吗?
class Foo:
def __init__(self):
(self.a, self.b) = foo_1()
self.c = foo_2()
def foo_1(self):
a = 0
b = 0
return (a, b)
def foo_2(self):
c = 0
return c
简而言之,在类中声明属性的约定是什么?
编辑:
当然,这是一个简单的示例,不需要在这里改进我的代码。想象一下一个复杂的类,在 _init_() 中调用了很多“小方法”(用于划分我的代码,以提高可读性)。
In Python, I can declare attributes all over the class. For example :
class Foo:
def __init__(self):
self.a = 0
def foo(self):
self.b = 0
It's difficult to retrieve all attributes in my class when I have a big class with a lot of attributes.
Is it better to have the following code (a) or the next following code (b) :
a) Here, it's difficult to locate all attributes :
class Foo:
def __init__(self):
foo_1()
foo_2()
def foo_1(self):
self.a = 0
self.b = 0
def foo_2(self):
self.c = 0
b) Here, it's easy to locate all attributes but is it beautiful ?
class Foo:
def __init__(self):
(self.a, self.b) = foo_1()
self.c = foo_2()
def foo_1(self):
a = 0
b = 0
return (a, b)
def foo_2(self):
c = 0
return c
In a nutshell, what is your conventions to declare your attributes in a class ?
EDIT:
Of course, it's a simple example and there's no need to improve my code here. Just imagine a complex class with a lot of "small methods" (to divide my code, to improve the readability) called in _init_().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
理想情况下,您的构造函数不应调用方法: 构造函数应该不做真正的工作。
如果属性数量增长不成比例,您应该重构您的类,例如使用 Extract Class 将您的类分成多个类,从而遵守单一职责原则。
Ideally, your constructor should not call methods: a constructor should not do real work.
If the number of attributes grows out of proportion, you should refactor your class, for example using Extract Class to break your class into several classes, thereby respecting the single responsibility principle.
这是什么问题:
或者如果
a
、b
和c
以某种方式相关,您可以在一行上声明它们:或者如果
a
、b
和c
以某种方式相关并且您将它们初始化为相同的不可变值,您可以在单个作业中声明它们:What's wrong with this:
Or if
a
,b
andc
are related in some way, you can declare them on a single line:Or if
a
,b
andc
are related in some way and you're initialising them to the same immutable value, you can declare them in a single assignment:首先,这里讨论的不是类属性,而是实例属性。我的约定是,但所有这些都在构造函数中,如果它们在另一个方法中初始化,则值为 None 。这样我总能知道会发生什么。这有点像声明它们,尽管这不是强制性的。
例如:
嗯,我也喜欢将初始化程序作为关键字参数,这使得它更加灵活。我也可以通过这种方式轻松覆盖默认值。
First of all, you are not talking about class attributes here, but instance attributes. My convention for that is to but all of them in the constructor, with the value of None if they are initialized in another method. That way I can always tell what to expect. It's sort of like declaring them, even though it isn't mandatory.
e.g.:
Well, I also like to make inititializers as keyword arguments, it makes it more flexible. I can easily override the defaults that way as well.
正确的。
没有。
dir(object)
或obj.__dict__
在那里很有帮助。此外,为什么要检索所有属性?一个好的编辑器/IDE 可以帮助您找到定义。我使用 WingIDE 来做这件事,它非常好。
好吧,这个特定的代码,也许不是,但是在
__init__
或类中声明所有公共属性是很常见的(但要注意 Jabberwock 当你这样做时)。这样您就知道它们可用,这很好。Correct.
Nope.
dir(object)
orobj.__dict__
are helpful there. And besides, why would you want to retrieve all the attributes?A good editor/IDE can help you find the definition. I use WingIDE for this, it's pretty good.
Well, this particular code, maybe not, but it is quite common to declare all public attributes in
__init__
or in the class (but beware the Jabberwock when you do this). That way you know they are available, which is good.