重点 (Top highlight)

At its core, Python is an object-oriented programming (OOP) language. Being an OOP language, Python handles data and functionalities by supporting various features centered around objects. For instance, data structures are all objects, including primitive types (e.g., integers and strings) which aren’t considered objects in some other languages. For another instance, functions are all objects, and they are merely attributes of other objects where they are defined (e.g., class or module).

Python的核心是一种面向对象的编程(OOP)语言。 作为一种OOP语言,Python通过支持围绕对象的各种功能来处理数据和功能。 例如,数据结构是所有对象,包括原始类型(例如,整数和字符串),在某些其他语言中这些原始类型不被视为对象。 对于另一个实例,函数是所有对象,它们仅仅是定义了它们的其他对象(例如,类或模块)的属性。

Although you can use built-in data types and write a bunch of functions without creating any custom classes, chances are that your code can become harder and harder to maintain when the project’s scope grows. These separate pieces have no shared themes, and there will be lots of hassles to manage connections between them, although much of the information is related.

尽管您可以使用内置数据类型并编写许多函数而无需创建任何自定义类,但是随着项目范围的扩大,您的代码可能会变得越来越难维护。 这些单独的部分没有共享的主题,并且尽管有很多信息是相关的,但要管理它们之间的连接仍会有很多麻烦。

In these scenarios, it’s worth defining your own classes, which will allow you to group related information and improve the structural design of your project. More importantly, the long-term maintainability of your codebase will be improved, because you’ll be dealing with less pieced code. However, there is a catch — this is only true when your class declaration is done in the right way such that the benefits of defining custom classes can outweigh the overhead of managing them.

在这些情况下,定义自己的类是值得的,这将使您可以对相关信息进行分组并改善项目的结构设计。 更重要的是,代码库的长期可维护性将得到改善,因为您将处理更少的分段代码。 但是,有一个陷阱-仅当您以正确的方式完成类声明时才如此,以至于定义自定义类的好处可能超过管理它们的开销。

In this article, I’d like to review nine important best practices that you should consider applying to your custom classes.

在本文中,我想回顾九种重要的最佳实践,您应该考虑将其应用于自定义类。

1.好名声 (1. Good Names)

When you’re defining your own class, you’re adding a new baby to your codebase. You should give the class a very good name. Although the only limit of your class name is the rules of a legal Python variable (e.g., can’t start with a number), there are preferred ways to give class names.

当您定义自己的类时,您要在代码库中添加一个新的婴儿。 您应该给全班一个很好的名字。 尽管类名的唯一限制是合法的Python变量的规则(例如,不能以数字开头),但是有一些首选的方法来提供类名。

  • Use nouns that are easy to pronounce. It’s especially important if you work on a team project. During a group presentation, you probably don’t want to be the person to say, “in this case, we create an instance of the Zgnehst class.” In addition, being easy to pronounce also means the name shouldn’t be too long. I can barely think of cases when you need to use more than three words to define a class name. One word is best, two words are good, and three words are the limit.

    使用易于发音的名词。 如果您在团队项目中工作,这一点尤其重要。 在小组演讲中,您可能不想成为这样的人:“在这种情况下,我们创建Zgnehst类的实例。” 另外,易于发音也意味着名称不应太长。 当您需要使用三个以上的单词来定义类名时,我几乎无法想到。 一个字是最好的,两个字是好,三个字是极限。

  • Reflect its stored data and intended functionalities. It’s like in our real life — boys are given boy names. When we see boy names, we expect the kids are boys. It applies to class names too (or any other variables in general). The rule is simple — Don’t surprise people. If you’re dealing with the students’ information, the class should be named Student. KiddosAtCampus isn’t making the most common sense.

    反映其存储的数据和预期的功能。 就像在我们的现实生活中一样-男孩被赋予男孩名字。 当我们看到男孩的名字时,我们期望孩子是男孩。 它也适用于类名(或通常的任何其他变量)。 规则很简单 -不要让人们感到惊讶。 如果您要处理学生的信息,则该班级应命名为Student。 KiddosAtCampus并不是最常识。

  • Follow naming conventions. We should use upper-case camel style for class names, like GoodName. The following is an incomplete list of unconventional class names: goodName, Good_Name, good_name, and GOodnAme. Following naming conventions is to make your intention clear. When people read your code, they can safely assume that an object with names like GoodName is a class.

    遵循命名约定。 我们应该对类名使用大写的驼色样式,例如GoodName。 以下是非常规类名称的不完整列表:goodName,Good_Name,good_name和GOodnAme。 遵循命名约定是为了使您的意图明确。 当人们阅读您的代码时,他们可以安全地假定名称如GoodName的对象是一个类。

There are also naming rules and conventions that apply to attributes and functions. In the below sections, I’ll briefly mention them where applicable, but the overall principles are the same. The only rule of thumb is simple: Don’t surprise people.

也有适用于属性和功能的命名规则和约定。 在以下各节中,我将在适用的地方简要提及它们,但是总体原理是相同的。 唯一的经验法则很简单:不要让人们感到惊讶。

2.显式实例属性 (2. Explicit Instance Attributes)

In most cases, we want to define our own instance initialization method (i.e., __init__). In this method, we set the initial state of our newly created instances of the class. However, Python doesn’t restrict where you can define instance attributes with custom classes. In other words, you can define additional instance attributes in later operations after the instance has been created. The following code shows you a possible scenario.

在大多数情况下,我们想定义自己的实例初始化方法(即__init__ )。 在此方法中,我们设置了新创建的类实例的初始状态。 但是,Python并没有限制您可以使用自定义类定义实例属性的位置。 换句话说,您可以在创建实例之后的后续操作中定义其他实例属性。 以下代码显示了一种可能的情况。

Initialization Method
初始化方法

As shown above, we can create an instance of the Student class by specifying a student’s first and last names. Later, when we call the instance method (i.e., verify_registration_status), the Student instance’s status attribute will be set. However, this isn’t the desired pattern, because if you spread various instance attributes throughout the entire class, you’re not making the class clear what data an instance object holds. Thus, the best practice is to place an instance’s attributes in the __init__ method, such that your code’s reader has a single place to get to know your class’s data structure, as shown below.

如上所示,我们可以通过指定学生的名字和姓氏来创建Student类的实例。 稍后,当我们调用实例方法(即verify_registration_status )时,将设置Student实例的status属性。 但是,这不是理想的模式,因为如果您在整个类中散布各种实例属性,则不会使类明确实例对象拥有的数据。 因此,最佳实践是将实例的属性放在__init__方法中,这样您的代码阅读器就可以通过一个地方来了解类的数据结构,如下所示。

Better Initialization Method
更好的初始化方法

For those instance attributes that you can’t set initially, you can set them with placeholder values, such as None. Although it’s of less concern, this change also helps prevent the possible error when you forget to call some instance methods to set the applicable instance attributes, causing AttributeError (‘Student’ object has no attribute ‘status_verified’).

对于最初无法设置的那些实例属性,可以使用占位符值(例如None 。 尽管'Student' object has no attribute 'status_verified'担心,但是当您忘记调用某些实例方法来设置适用的实例属性时,此更改还有助于防止可能的错误,从而导致AttributeError ( 'Student' object has no attribute 'status_verified' )。

In terms of the naming rules, the attributes should be named using lower cases and follow the snake case style, which means that if you use multiple words, connect them with underscores. Moreover, all the names should have meaningful indication regarding what data it holds (e.g., first_name is better than fn).

就命名规则而言,属性应使用小写字母命名,并遵循蛇形字母样式,这意味着如果您使用多个单词,请使用下划线将它们连接起来。 此外,所有名称都应具有关于所保存数据的有意义的指示(例如, first_namefn更好)。

3.使用属性-但精简 (3. Use Properties — But Parsimoniously)

Some people learn Python coding with an existing background of other OOP languages, such as Java, and they’re used to creating getters and setters for attributes of the instances. This pattern can be mimicked with the use of the property decorator in Python. The following code shows you the basic form of using the property decorator to implement getters and setters.

有些人在其他OOP语言(例如Java)的现有背景下学习Python编码,并且习惯于为实例的属性创建getter和setter。 可以通过在Python中使用属性装饰器来模仿此模式。 以下代码显示了使用属性装饰器实现getter和setter的基本形式。

Property Decorator
物业装饰

Once this property is created, we can use it as regular attributes using the dot notation, although it’s implemented using functions under the hood.

创建此属性后,我们可以使用点符号将其用作常规属性,尽管它是通过内部函数实现的。

Use Properties
使用属性

As you may know, the advantages of using property implementations include verification of proper value settings (check a string is used, not an integer) and read-only access (by not implementing the setter method). However, you should use properties parsimoniously. It can be very distracting if your custom class looks like the below — there are too many properties!

如您所知,使用属性实现的优点包括验证正确的值设置(检查是否使用字符串,而不是整数)和只读访问权限(通过不实现setter方法)。 但是,您应该同时使用属性。 如果您的自定义类如下所示,可能会很让人分心–属性太多!

Abuse of Properties
滥用财产

In most cases, these properties can be replaced with instance attributes, and thus we can access them and set them directly. Unless you have specific needs for the benefits of using properties as discussed (e.g., value verification), using attributes is preferred over creating properties in Python.

在大多数情况下,这些属性可以用实例属性代替,因此我们可以访问它们并直接设置它们。 除非您对使用属性(如讨论的值)的好处有特定的需求,否则使用属性优先于在Python中创建属性。

4.定义有意义的字符串表示 (4. Define Meaningful String Representations)

In Python, functions that have double underscores before and after the name are referred to as special or magic methods, and some people call them dunder methods. They have special usages for basic operations by the interpreter, including the __init__ method that we’ve covered previously. Two special methods, __repr__ and __str__, are essential for creating proper string representations of your custom class, which will give the code readers more intuitive information about your classes.

在Python中,名称前后带有双下划线的函数称为特殊方法或魔术方法,有些人将其称为dunder方法。 它们对解释器的基本操作有特殊的用法,包括我们先前介绍的__init__方法。 两种特殊方法__repr____str__对于创建自定义类的正确字符串表示形式至关重要,这将为代码阅读器提供有关类的更直观信息。

Between them, the major difference is that the __repr__ method defines the string, using which you can re-create the object by calling eval(repr(“the repr”)), while the __str__ method defines the string that is more descriptive and allows more customization. In other words, you can think that the string defined in the __repr__ method is to be viewed by developers while that used in the __str__ method is to be viewed by regular users. The following shows you an example.

它们之间的主要区别在于__repr__方法定义了字符串,使用该字符串可以通过调用eval(repr(“the repr”))来重新创建对象,而__str__方法定义的字符串则更具描述性并允许更多定制。 换句话说,您可以认为__repr__方法中定义的字符串将由开发人员查看,而__str__方法中使用的__str__将由常规用户查看。 下面显示了一个示例。

Implementation of String Representations
字符串表示的实现

Please note that in the __repr__ method’s implementation (Line 7), the f-string uses !r which will show these strings with quotation marks, because they’re necessary to construct the instance with strings properly formatted. Without the !r formatting, the string will be Student(John, Smith), which isn’t the correct way to construct a Student instance. Let’s see how these implementations show the strings for us. Specifically, the __repr__ method is called when you access the object in the interactive interpreter, while the __str__ method is called by default when you print the object.

请注意,在__repr__方法的实现(第7行)中,f字符串使用!r ,它将用引号显示这些字符串,因为使用正确格式的字符串构造实例是必需的。 如果没有!r格式,则字符串将为Student(John, Smith) ,这不是构造Student实例的正确方法。 让我们看看这些实现如何为我们显示字符串。 具体来说, __repr__而该方法时,您在交互式解释器访问该对象调用, __str__方法默认情况下,当你打印的对象调用。

String Representations
字符串表示

5.实例,类和静态方法 (5. Instance, Class, and Static Methods)

In a class, we can define three kinds of methods: instance, class, and static methods. You need to consider what methods you should use for the functionalities of concern. Here are some general guidelines.

在一个类中,我们可以定义三种方法:实例,类和静态方法。 您需要考虑针对所关注的功能应使用哪些方法。 以下是一些一般准则。

If the methods are concerned with individual instance objects, for example, you need to access or update particular attributes of an instance, in which cases, you should use instance methods. These methods have a signature like this: def do_something(self):, in which the self argument refers to the instance object that calls the method. To know more about the self argument, you can refer to my previous article on this topic.

例如,如果方法与单个实例对象有关,则需要访问或更新实例的特定属性,在这种情况下,应使用实例方法。 这些方法具有如下签名: def do_something(self):其中self参数引用调用该方法的实例对象。 要了解有关self参数的更多信息,可以参考我以前关于该主题的文章 。

If the methods are not concerned with individual instance objects, you should consider using class or static methods. Both methods can be easily defined with applicable decorators: classmethod and staticmethod. The difference between these two is that class methods allow you to access or update attributes related to the class, while static methods are independent of any instance or the class itself. A common example of a class method is providing a convenience instantiation method, while a static method can be simply a utility function. The following code shows you some examples.

如果方法与单个实例对象无关,则应考虑使用类或静态方法。 可以使用适用的修饰符轻松定义这两种方法: classmethodstaticmethod 。 两者之间的区别在于,类方法允许您访问或更新与类相关的属性,而静态方法则独立于任何实例或类本身。 类方法的一个常见示例是提供一种方便的实例化方法,而静态方法可以只是一个实用函数。 以下代码为您提供了一些示例。

Different Kinds of Methods
不同种类的方法

In a similar fashion, you can also create class attributes. Unlike instance attributes that we discussed earlier, class attributes are shared by all instance objects, and they should reflect some characteristics independent of individual instance objects.

您也可以类似的方式创建类属性。 与前面讨论的实例属性不同,类属性由所有实例对象共享,并且它们应反映一些独立于各个实例对象的特征。

6.使用私有属性进行封装 (6. Encapsulation Using Private Attributes)

When you write custom classes for your project, you need to take into account encapsulation, especially if you’re expecting that others will use your classes too. When the functionalities of the class grow, some functions or attributes are only relevant for data processing within the class. In other words, outside the class, these functions won’t be called and other users of your class won’t even care about the implementation details of these functions. In these scenarios, you should consider encapsulation.

在为项目编写自定义类时,需要考虑封装性,尤其是在期望其他人也将使用您的类的情况下。 当类的功能增长时,某些功能或属性仅与类内的数据处理相关。 换句话说,在类之外,将不会调用这些函数,并且您的类的其他用户甚至不会关心这些函数的实现细节。 在这些情况下,您应该考虑封装。

One important way to apply encapsulation is to prefix attributes and functions with an underscore or two underscores, as a convention. The subtle difference is that those with an underscore are considered protected, while those with two underscores are considered private, which involves name-mangling after its creation. Differentiating these two categories is beyond the scope of the present article, and one of my previous articles have covered them.

按照惯例,应用封装的一种重要方法是为属性和函数加上下划线或两个下划线。 细微的区别是带有下划线的人被认为是受保护的 ,而带有两个下划线的人被认为是private ,这涉及在其创建后进行名称处理。 区分这两个类别不在本文的讨论范围之内,而我以前的文章之一已经涵盖了它们。

In essence, by naming attributes and functions this way, you’re telling the IDEs (i.e., integrated development environment, such as PyCharm) that they’re not going to be accessed outside the class, although true private attributes don’t exist in Python. In other words, they’re still accessible if we choose so.

本质上,通过这样命名属性和函数,是在告诉IDE(即集成开发环境,例如PyCharm),尽管在类中不存在真正的私有属性,但它们不会在类外部访问。Python。 换句话说,如果我们选择它们,它们仍然可以访问。

Encapsulation
封装形式

The above code shows you a trivial example of encapsulation. For a student, we may be interested in knowing their average GPA, and we can get the point using the get_mean_gpa method. The user doesn’t need to know how the mean GPA is calculated, such that we can make related methods protected by placing an underscore prefixing the function names.

上面的代码向您展示了一个简单的封装示例。 对于一个学生,我们可能会对了解他们的平均GPA感兴趣,我们可以使用get_mean_gpa方法获得get_mean_gpa 。 用户不需要知道平均GPA的计算方式,因此我们可以通过在函数名称前添加下划线来保护相关方法。

The key takeaway for this best practice is that you expose only the minimal number of public APIs that are relevant for the users to use your code. For those that are used only internally, make them protected or private methods.

此最佳实践的主要收获是,您仅公开与用户使用您的代码相关的最少数量的公共API。 对于仅在内部使用的那些,请将其设置为受保护的方法或私有方法。

7.单独的关注点和解耦 (7. Separate Concerns and Decoupling)

With the development of your project, you find out that you’re dealing with more data, your class can become cumbersome if you’re sticking to one single class. Let’s continue with the example of the Student class. Suppose that students have lunch at school, and each of them has a dining account that they can use to pay for meals. Theoretically, we can deal with account-related data and functionalities within the Student class, as shown below.

随着项目的发展,您发现自己正在处理更多的数据,如果您坚持一个单一的班级,那么您的班级可能会变得很麻烦。 让我们继续学生类的示例。 假设学生在学校吃午餐,并且每个人都有一个餐饮帐户,可以用来支付餐费。 从理论上讲,我们可以处理Student类中与帐户相关的数据和功能,如下所示。

Mixed Functionalities
混合功能

The above code shows you some pseudocode on checking account balance and loading money to the account, both of which are implemented in the Student class. Imagine that there are more operations that can be related to the account, such as suspending a lost card, consolidating accounts — to implement all of them will make the Student class larger and larger, which make it gradually more difficult to maintain. Instead, you should isolate these responsibilities and make your Student class irresponsible for these account-related functionalities — a design pattern termed as decoupling.

上面的代码为您提供了一些有关检查帐户余额和向帐户充值的伪代码,这两种代码均在Student类中实现。 想象一下,还有更多与该帐户相关的操作,例如暂停丢失的卡,合并帐户-实施所有这些操作将使Student类越来越大,从而使维护学生的难度逐渐增加。 相反,您应该隔离这些责任,并使您的学生班级对这些与帐户相关的功能不负责任-一种称为脱钩的设计模式。

Separated Concerns
分离的问题

The above code shows you how we can design the data structures with an additional Account class. As you can see, we move all account-related operations into the Account class. To retrieve the account information for the student, the Student class will handle the functionality by retrieving information from the Account class. If we want to implement more functions related to the class, we can simply update the Account class only.

上面的代码向您展示了如何使用附加的Account类设计数据结构。 如您所见,我们将所有与帐户相关的操作移至Account类。 要检索学生的帐户信息, Student类将通过从Account类中检索信息来处理该功能。 如果我们想实现更多与该类相关的功能,我们只需简单地更新Account类即可。

The main takeaway for the design pattern is that you want your individual classes to have separate concerns. By having these responsibilities separated, your classes become smaller, which makes future changes easier, because you’ll be dealing with smaller code components.

设计模式的主要要点是,您希望各个类具有单独的关注点。 通过将这些职责分开,您的类将变小,这将使将来的更改变得更容易,因为您将处理较小的代码组件。

8.考虑__slots__进行优化 (8. Consider __slots__ For Optimization)

If your class is used mostly as data containers for storing data only, you can consider using __slots__ to optimize the performance of your class. It doesn’t only increase the speed of attribute accessing but also saves memory, which can be a big benefit if you need to create thousands or many more instance objects. The reason is that for a regular class, instance attributes are stored through an internally managed dictionary. By contrast, with the use of the __slots__, instance attributes will be stored using array-related data structures implemented using C under the hood, and their performance is optimized with much higher efficiency.

如果您的类主要用作仅用于存储数据的数据容器,则可以考虑使用 __slots__ 以优化您的班级表现。 它不仅可以提高属性访问的速度,而且可以节省内存,如果您需要创建数千个或更多实例对象,这将是一个很大的好处。 原因是对于常规类,实例属性是通过内部托管的字典存储的。 相比之下,通过使用__slots__ ,实例属性将使用在__slots__使用C实现的与数组相关的数据结构存储,并且以更高的效率优化了它们的性能。

Use of __slots__ in Class Definition
在类定义中使用__slots__

The above code shows you a trivial example of how we implement the __slots__ in a class. Specifically, you list all the attributes as a sequence, which will create a one-to-one match in data storage for faster access and less memory consumption. As just mentioned, regular classes use a dictionary for attribute accessing but not for those with __slots__ implemented. The following code shows you such a fact.

上面的代码显示了一个简单的示例,说明了我们如何在类中实现__slots__ 。 具体来说,您将所有属性列为一个序列,这将在数据存储中创建一对一匹配,以加快访问速度并减少内存消耗。 如前所述,常规类使用字典进行属性访问,但不使用实现__slots__的字典。 以下代码向您展示了这样一个事实。

No __dict__ in Classes With __slots__
具有__插槽__的类中没有__dict__

A detailed discussion of using __slots__ can be found in a nice answer on Stack Overflow, and you can find more information from the official documentation. Regarding the gained benefits of faster access and saved memory, a recent Medium article has a very good demonstration, and I’m not going to expand on this. However, one thing to note is that using __slots__ will have a side effect — it prevents you from dynamically creating additional attributes. Some people propose it as a mechanism for controlling what attributes your class has, but it’s not how it was designed.

有关使用__slots__详细讨论,可以在Stack Overflow上找到一个不错的答案,您可以从官方文档中找到更多信息。 关于更快访问和节省内存所获得的好处, 最近的《中型》文章很好地演示了这一点,我将不对此进行扩展。 但是,需要注意的一件事是,使用__slots__会产生副作用-它会阻止您动态创建其他属性。 有人提出将其作为一种控制类具有的属性的机制,但这并不是它的设计方式。

9.文件 (9. Documentation)

Last, but not least, we have to talk about documentation of your class. Most importantly, we need to understand that writing documents isn’t replacing any code. Writing tons of documents doesn’t improve your code’s performance, and it doesn’t necessarily make your code more readable. If you have to rely on docstrings to clarify your code, it’s very likely that your code has problems. I truly believe that your code should speak all by itself. The following code just shows you a mistake that some programmers can make — using unnecessary comments to compensate for bad code (i.e., meaningless variable names in this case). By contrast, some good code with good names doesn’t even need comments.

最后但并非最不重要的一点,我们必须谈论您的课程的文档。 最重要的是,我们需要了解编写文档并不能替代任何代码。 编写大量文档并不能提高代码的性能,也不一定会使代码更具可读性。 如果您必须依靠文档字符串来澄清代码,则很可能您的代码有问题。 我真的相信您的代码应该自己讲。 以下代码仅向您显示一些程序员可能犯的一个错误-使用不必要的注释来补偿错误的代码(即,在这种情况下,无意义的变量名)。 相比之下,一些带有好名字的好代码甚至不需要注释。

Bad Comment Examples
不良评论示例

I’m not saying that I’m against writing comments and docstrings, but it really depends on your use cases. If your code is used by more than one person or more than one occasion (e.g., you’re the only one accessing the code but for multiple times), you should consider writing some good comments. They can help yourself or your teammates read your code, but no one should assume that your code does exactly what’s said in the comments. In other words, writing good code is always the top priority that you need to keep in mind.

我并不是说我反对写注释和文档字符串,但这实际上取决于您的用例。 如果您的代码被不止一个人使用或多次使用(例如,您是唯一一次访问代码但多次访问的代码),则应考虑编写一些好的注释。 他们可以帮助您自己或您的队友阅读您的代码,但是没有人可以假设您的代码完全符合注释中的要求。 换句话说,编写好的代码始终是您需要牢记的头等大事。

If particular portions of your code are to be used by end users, you want to write docstrings, because those people aren’t familiar with the relevant codebase. All they want to know is how to use the pertinent APIs, and the docstrings will form the basis for the help menu. Thus, it’s your responsibility as the programmer to make sure that you provide clear instructions on how to use your programs.

如果最终用户要使用代码的特定部分,则需要编写文档字符串,因为这些人对相关的代码库并不熟悉。 他们只想知道如何使用相关的API,而文档字符串将构成帮助菜单的基础。 因此,作为程序员,您有责任确保提供有关如何使用程序的明确说明。

结论 (Conclusions)

In this article, we reviewed important factors that you need to consider when you define your own classes. If you’re new to Python or programming in general, you may not fully understand every aspect that we’ve discussed, which is OK. The more you code, the more you’ll find the importance of having these principles in mind before you define your classes. Practice these guidelines continuously when you work with classes because a good design will save much of your development time later.

在本文中,我们回顾了定义自己的类时需要考虑的重要因素。 如果您不熟悉Python或一般编程,那么您可能不会完全了解我们所讨论的各个方面,这没关系。 您编写的代码越多,您就越会发现在定义类之前牢记这些原则的重要性。 在上课时,请不断练习这些准则,因为好的设计会在以后节省很多开发时间。

翻译自: https://medium.com/better-programming/advanced-python-9-best-practices-to-apply-when-you-define-classes-871a27af658b


http://www.taodudu.cc/news/show-994822.html

相关文章:

  • 医疗大数据处理流程_我们需要数据来大规模改善医疗流程
  • python对象引用计数器_在Python中借助计数器对象对项目进行计数
  • 数字图像处理 python_5使用Python处理数字的高级操作
  • 软件测试框架课程考试_那考试准备课程值得吗?
  • 为什么在Python代码中需要装饰器
  • 数据清理最终实现了自动化
  • Python气流介绍
  • 正确的词典访问方式
  • 废水处理计算书 excel_废水监测数据是匿名的吗?
  • 数据科学还是计算机科学_您应该拥有数据科学博客的3个原因
  • 熊猫分发_流利的熊猫
  • python记录日志_5分钟内解释日志记录—使用Python演练
  • p值 t值 统计_非统计师的P值
  • 如何不部署Keras / TensorFlow模型
  • 对食材的敬畏之心极致产品_这些数据科学产品组合将给您带来敬畏和启发(2020年中的版本)
  • 向量积判断优劣弧_判断经验论文优劣的10条诫命
  • sql如何处理null值_如何正确处理SQL中的NULL值
  • 数据可视化 信息可视化_动机可视化
  • 快速数据库框架_快速学习新的数据科学概念的框架
  • 停止使用p = 0.05
  • 成像数据更好的展示_为什么更多的数据并不总是更好
  • vue domo网站_DOMO与Tableau-逐轮
  • 每个人都应该使用的Python 3中被忽略的3个功能
  • 数据探查_数据科学家,开始使用探查器
  • 从ncbi下载数据_如何从NCBI下载所有细菌组件
  • 线性插值插值_揭秘插值搜索
  • 如果您不将Docker用于数据科学项目,那么您将生活在1985年
  • docker部署flask_使用Docker,GCP Cloud Run和Flask部署Scikit-Learn NLP模型
  • 问卷 假设检验 t检验_真实问题的假设检验
  • 大数据技术 学习之旅_为什么聚焦是您数据科学之旅的关键

高级Python:定义类时要应用的9种最佳做法相关推荐

  1. python定义类时括号_为什么在定义类时括号是可选的,而在定义函数时括号是必需的?...

    我认为你的问题的答案只是语法.这正是Python的设置方式,但我对它的设置方式的看法是: 我认为函数来自数学,比如:f(x) = x 所以当计算机编程语言被创造出来的时候,从模拟数学到编程语言似乎有某 ...

  2. python 定义类时,内部方法的互相调用

    每次调用内部的方法时,方法前面加 self. 举例: 例子参考百度知道里面的回答 class MyClass:def __init__(self):passdef func1(self):# do s ...

  3. python中表示类的公有成员_在Python中定义类时,如果某个成员名称前有2个下划线则表示是私有成员。...

    在Python中定义类时,如果某个成员名称前有2个下划线则表示是私有成员. 更多相关问题 [填空题] 汽车板金的一般修理工艺是:校正.敲平.().().焊接及点焊. [填空题] 汽车照明系统主要由() ...

  4. python 定义list长度_python中list列表的高级函数 python如何统计列表的长度

    在python的函数中,如何将列表list的一部分作为函比如定义个函数,想实现的功能就是将列表a的后半部分(['c','d'])传入后面paraTestList(a[2:])中,括号里面的a[2:]命 ...

  5. 在python中定义类时实例方法的第一个参数名称_第7.5节 揭开Python类中self的面纱...

    在上节已经引入介绍了类定义,并简单介绍了类变量.实例变量.类方法和实例方法,后面章节还会进一步详细介绍相关的内容.本节要介绍的self是与类变量.实例变量.类方法和实例方法具体定义实现强相关的. 一. ...

  6. 数据科学Python训练营课程:从初级到高级 Python for Data Science Bootcamp Course:Beginner to Advanced

    通过代码实现.示例等,掌握您需要了解的关于Python.Pandas和Numpy的一切! 你会学到什么 通过代码实现.示例等,掌握您需要了解的关于Python.Pandas和Numpy的一切! 学习高 ...

  7. python定义类()中写object和不写的区别

    python定义类()中写object和不写的区别 这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python ...

  8. GO语言的进阶之路-Golang高级数据结构定义

    GO语言的进阶之路-Golang高级数据结构定义 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们之前学习过Golang的基本数据类型,字符串和byte,以及rune也有所了解, ...

  9. 6个高级Python技巧

    前言 Python是一种面向对象的语言,它与英语非常相似,因此对于初学者来说是一种非常好的语言.它的高级特性和受支持的库包甚至可以用几行代码来编写复杂的任务.在本文中,我们将介绍python的一些高级 ...

最新文章

  1. c语言程序设计网课作业答案,《C语言程序设计》作业答案
  2. C# 打开文件/跳转链接
  3. python指定目录生成.csv文件_python文件处理-根据csv文件内容,将对应图像拷贝到指定文件夹...
  4. Spring框架中XML配置文件注入集合(数组、LIST、MAP、SET)属性
  5. oracle的sql口令,Oracle数据库的SYS口令是什么?
  6. VIM+LaTexSuite配置
  7. Component 初识组件
  8. 微信支付提示微信登录失败:redirect_uri域名与后台配置不一致,错误码:10003
  9. mariadb特有函数
  10. 敏捷开发的PRD该怎么写
  11. Rockchip官方开发板的Android固件下载地址汇总
  12. JS 怎么将一个对象的值作为另一个对象的key
  13. 日常英语---九、MapleStory Link Skills Guide
  14. Day 17 - YOLO 相关概念说明
  15. Recovering a NOARCHIVELOG Database with Incremental Backups
  16. 单片机按键去抖,极简
  17. AlexNet网络实现
  18. 十行 js 获取带有双字节字符的字符串长度
  19. 无公网IP,外网远程连接MySQL数据库
  20. 活捉那只抢算力的谷歌员工!挤占计算资源?博弈论或可破解数据中心“囚徒困境”

热门文章

  1. 【算法】学习笔记(1):算法就是人类去教会计算机的方法
  2. 【微机原理与接口技术】多功能可编程芯片 与 多功能电饭煲
  3. 10-排序6 Sort with Swap(0, i) (25 分)
  4. vue 中v-if 与v-show 的区别
  5. Android APK 打包过程 MD
  6. ubuntu上如何安装tomcat
  7. 教辅的组成(网络流果题 洛谷P1231)
  8. 4.kafka的安装部署
  9. OC extern和变量
  10. mac vagrant 虚拟机nfs挂载点