简体   繁体   English

请帮助我了解Python中的类

[英]Please help me in understanding Class in Python

I am newbie and finding it very hard to grasp the syntax of Class in python. 我是新手,发现很难掌握python中Class的语法。 I have a background of C/C++, java and objective C. A very big difference which i am noticing in python is that you don't explicitly declare the "data members" in the class and you just randomly add them? 我有C / C ++,java和目标C的背景。我在python中注意到的一个很大的不同是您没有在类中显式声明“数据成员”,而只是随机地添加它们? And it leads to quite big confusion. 这会导致很大的混乱。

Let say i have a class 假设我有一堂课

class MyClass:

    def __int__(self, a, b):

        self.a = a

        self.b = b

And then when i initiate the object. 然后当我启动对象时。

myobject = MyClass(10,10)

And just after some time for some reason i come to know that i need another parameter in this class but i dont wanted to initiate that using constructor because it will be initiated by another function depending on the some particular condition, so in whole mess of code that will be only point that variable actually get birth. 一段时间后,由于某种原因,我开始知道我需要在此类中使用另一个参数,但我不想使用构造函数来初始化该参数,因为它将由另一个函数根据某些特定条件来初始化,因此整个代码混乱不堪那只是变量实际诞生的关键。 is not the case when i will be checking the code while debugging or reviewing it for some other reason it will be confusing? 是不是我在调试或其他原因检查代码时会感到困惑?

In short, Yes . 简而言之,

You're right. 你是对的。 Python lets you add (and remove!) members from objects at will, at any time. Python使您可以随时从对象中随意添加(或删除!)成员。 There's nothing special about a constructor that allows it to do anything that other functions can't. 构造函数没有什么特别的,它可以执行其他函数无法执行的任何操作。

If you want to be sure that all instances of your class have the same members at all times, then by all means assign them all in the constructor, using a sentinel value like None for ones that don't have a meaningful value yet, and avoid adding new members outside the constructor. 如果要确保您的类的所有实例始终具有相同的成员,则一定要在构造函数中全部分配它们,并使用诸如None的前哨值将那些尚未具有有意义值的值赋给它们;以及避免在构造函数外部添加新成员。

It's up to you how you manipulate your objects, and if you want to do that in a static fashion then that's fine, or if you want to take advantage of the ability to add and remove members at arbitrary times, that's fine too. 这取决于您如何操作对象,如果您想以静态方式进行操作,那很好,或者如果您想利用在任意时间添加和删除成员的能力,那也很好。 Python itself doesn't impose (m)any rules. Python本身不施加任何规则。

You should really use some . 您确实应该使用一些 in your text :p 在您的文字中:p

Could you mean: 您的意思是:

class MyClass:
    def __int__(self, a, b, c=None):
        self.a = a
        self.b = b
        self.c = c

one = MyClass(1,2)
one.c # None
two = MyClass(1,2,3)
two.c # 3
class MyClass:
    def __int__(self, a, b):
        self.a = a
        self.b = b
        self.c = None  #This line is optional

    def set_c(self, c):
        self.c = c

Some people prefer to list all the attributes in the __init__ . 有些人喜欢列出__init__中的所有属性。 You don't have to, but there are any number of reasons you might choose to. 您不必一定要这样做,但是有很多原因可能会导致您选择。
Maybe it improves your editor's ability to understand your code for highlighting or completion. 也许它可以提高您的编辑器理解突出显示或完成代码的能力。
Maybe it is just a style that you prefer. 也许这只是您喜欢的样式。

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

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