Python - 在类中声明属性的约定是什么?

发布于 2024-10-09 21:01:11 字数 893 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇见了你 2024-10-16 21:01:11

理想情况下,您的构造函数不应调用方法: 构造函数应该不做真正的工作

如果属性数量增长不成比例,您应该重构您的类,例如使用 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.

最好是你 2024-10-16 21:01:11

这是什么问题:

class Foo:
    def __init__(self):
        self.a = 0
        self.b = 1
        self.c = 2

或者如果 abc 以某种方式相关,您可以在一行上声明它们:

class Foo:
    def __init__(self):
        self.a, self.b, self.c = 0, 1, 2

或者如果abc 以某种方式相关并且您将它们初始化为相同的不可变值,您可以在单个作业中声明它们:

class Foo:
    def __init__(self):
        self.a = self.b = self.c = 0

What's wrong with this:

class Foo:
    def __init__(self):
        self.a = 0
        self.b = 1
        self.c = 2

Or if a, b and c are related in some way, you can declare them on a single line:

class Foo:
    def __init__(self):
        self.a, self.b, self.c = 0, 1, 2

Or if a, b and c are related in some way and you're initialising them to the same immutable value, you can declare them in a single assignment:

class Foo:
    def __init__(self):
        self.a = self.b = self.c = 0
请别遗忘我 2024-10-16 21:01:11

首先,这里讨论的不是类属性,而是实例属性。我的约定是,但所有这些都在构造函数中,如果它们在另一个方法中初始化,则值为 None 。这样我总能知道会发生什么。这有点像声明它们,尽管这不是强制性的。

例如:

class Foo(object):
    def __init__(self, a=None, b=None, c=None):
        self.a = a
        self.b = b
        self.c = c

嗯,我也喜欢将初始化程序作为关键字参数,这使得它更加灵活。我也可以通过这种方式轻松覆盖默认值。

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.:

class Foo(object):
    def __init__(self, a=None, b=None, c=None):
        self.a = a
        self.b = b
        self.c = c

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.

忆沫 2024-10-16 21:01:11

在Python中,我可以声明属性
遍布全班。

正确的。

很难全部检索到
当我有一个类时,我的类中的属性
具有很多属性的大类。

没有。 dir(object)obj.__dict__ 在那里很有帮助。此外,为什么要检索所有属性?

在这里,很难找到所有
属性

一个好的编辑器/IDE 可以帮助您找到定义。我使用 WingIDE 来做这件事,它非常好。

在这里,很容易找到所有
属性但是它漂亮吗?

好吧,这个特定的代码,也许不是,但是在 __init__ 或类中声明所有公共属性是很常见的(但要注意 Jabberwock 当你这样做时)。这样您就知道它们可用,这很好。

In Python, I can declare attributes
all over the class.

Correct.

It's difficult to retrieve all
attributes in my class when I have a
big class with a lot of attributes.

Nope. dir(object) or obj.__dict__ are helpful there. And besides, why would you want to retrieve all the attributes?

Here, it's difficult to locate all
attributes

A good editor/IDE can help you find the definition. I use WingIDE for this, it's pretty good.

Here, it's easy to locate all
attributes but is it beautiful ?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文