Python学习笔记(二十六)- 类编写基础 Class Coding Basics

1.class如何与模块相关?
答:类总是嵌套在模块中;它们是模块(moudule)对象的属性。类和模块都是命名空间,但类对应于语句(而不是整个文件),并且支持多个实例,继承和运算符重载的OOP概念(模块不支持)。从某种意义上说,模块就像一个单实例类,没有继承,它对应于整个代码文件。

2.如何创建实例和类?
答:通过运行class语句来创建类;通过调用类来创建实例,这个过程就好像它是一个函数一样。

3.类属性在何处以及如何创建?
答:通过将属性分配给类对象来创建类属性。它们通常由嵌套在类语句中的顶级赋值语句生成。在类语句块中分配的每个名称都成为类对象的属性(从技术上讲,类语句的局部作用域转换为类对象的属性命名空间,非常类似于模块) 。但是,也可以通过在类对象的引用存在的任何地方为类分配属性来创建类属性 - 甚至在类语句之外。

4.实例属性在何处以及如何创建?
答:通过将属性分配给实例对象来创建实例属性。它们通常是在类声明中编写的类的方法函数中创建的,方法是将属性赋值给self参数(它始终是隐含的实例)。但是,同样,它们可以通过在实例引用出现的任何地方创建,甚至在类语句之外。通常,所有实例属性都在__init__构造函数方法中初始化;这样,以后的方法调用可以假设属性已经存在。

5. Python类中的self意味着什么?
答:self是类的方法函数中第一个(最左边)参数的通常名称; Python自动使用实例对象填充它,该实例对象是方法调用的隐含主体。这个参数不需要被称为self(虽然这是一个非常强烈的惯例);它的地位是重要的。 (前C ++或Java程序员可能更喜欢这样称呼它,因为在那些名称反映相同想法的语言中;但在Python中,这个参数必须始终是明确的。)

6.如何在Python类中编写运算符重载(operator overloading)?
答:运算符重载在Python类中使用特殊命名的方法编写;它们都以双下划线开始和结束,使它们独一无二。这些不是内置或保留的名称;当实例出现在相应的操作中时,Python会自动运行它们。 Python本身定义了从操作到特殊方法名称的映射。

扫描二维码关注公众号,回复: 5458178 查看本文章


7.您何时可能希望在类中支持运算符重载?
答:运算符重载对于实现类似于内置类型的对象(例如,序列或诸如矩阵的数字对象)以及模仿一段代码所期望的内置类型接口是有用的。模仿内置类型接口使您能够传入也具有状态信息的类实例(即,在操作调用之间记住数据的属性)。但是,当一个简单的命名方法就足够时,你不应该使用运算符重载。

8.哪种运算符重载方法最常用?
答:__init__构造函数方法是最常用的;几乎每个类都使用此方法为实例属性设置初始值并执行其他启动任务。

9.理解Python OOP代码需要哪两个关键概念?
答:方法函数中的特殊self参数和__init__构造函数方法是Python中OOP代码的两个基石;如果你得到这些,你应该能够阅读大多数OOP Python代码的文本 - 除此之外,它主要仅仅是函数包。当然,继承搜索也很重要,但self表示自动对象参数,而__init__很普遍。

其它备注(有关self的意义):

实际上,这是 self 参数必须始终在Python方法中显式的原因之一 - 因为方法可以创建为独立于类的简单函数,它们需要使隐含的实例参数显式化。 它们可以被称为函数或方法,Python既不能猜测也不能假设简单函数最终可能成为类的方法。 然而,显式 self 参数的主要原因是使名称的含义更加明显:未通过 self 引用的名称是映射到作用域的简单变量,而通过带有属性符号的 self 引用的名称显然是实例属性。

In fact, this is one of the reasons the self argument must always be explicit in Python methods—because
methods can be created as simple functions independent of a class, they need to make the implied instance
argument explicit. They can be called as either functions or methods, and Python can neither guess nor
assume that a simple function might eventually become a class’s method. The main reason for the explicit
self argument, though, is to make the meanings of names more obvious: names not referenced through
self are simple variables mapped to scopes, while names referenced through self with attribute notation
are obviously instance attributes.

注:转载《Learning Python 5th Edition》[奥莱理]

1. How are classes related to modules?
2. How are instances and classes created?
3. Where and how are class attributes created?
4. Where and how are instance attributes created?
5. What does self mean in a Python class?
6. How is operator overloading coded in a Python class?
7. When might you want to support operator overloading in your classes?
8. Which operator overloading method is most commonly used?
9. What are two key concepts required to understand Python OOP code?

1. Classes are always nested inside a module; they are attributes of a module object. Classes and modules are both namespaces, but classes correspond to statements (not entire files) and support the OOP notions of multiple instances, inheritance, and operator overloading (modules do not). In a sense, a module is like a single instance class, without inheritance, which corresponds to an entire file of code.
2. Classes are made by running class statements; instances are created by calling a class as though it were a function.
3. Class attributes are created by assigning attributes to a class object. They are normally generated by top-level assignments nested in a class statement each name assigned in the class statement block becomes an attribute of the class object (technically, the class statement's local scope morphs into the class object's attribute namespace, much like a module). Class attributes can also be created, though, by assigning attributes to the class anywhere a reference to the class object exists—even outside the class statement.
4. Instance attributes are created by assigning attributes to an instance object. They are normally created within a class's method functions coded inside the class statement, by assigning attributes to the self argument (which is always the implied instance). Again, though, they may be created by assignment anywhere a reference to the instance appears, even outside the class statement. Normally, all instance attributes are initialized in the __init__ constructor method; that way, later method calls can assume the attributes already exist.
5. self is the name commonly given to the first (leftmost) argument in a class's method function; Python automatically fills it in with the instance object that is the implied subject of the method call. This argument need not be called self (though this is a very strong convention); its position is what is significant. (Ex-C++ or Java programmers might prefer to call it this because in those languag that name reflects the same idea; in Python, though, this argument must always be explicit.)
6. Operator overloading is coded in a Python class with specially named methods; they all begin and end with double underscores to make them unique. These are not built-in or reserved names; Python just runs them automatically when an instance appears in the corresponding operation. Python itself defines the mappings from operations to special method names.
7. Operator overloading is useful to implement objects that resemble built-in types (e.g., sequences or numeric objects such as matrixes), and to mimic the built-in type interface expected by a piece of code. Mimicking built-in type interfaces enables you to pass in class instances that also have state information (i.e., attributes that remember data between operation calls). You shouldn't use operator overloading when a simple named method will suffice, though.
8. The __init__ constructor method is the most commonly used; almost every class uses this method to set initial values for instance attributes and perform other startup tasks.
9. The special self argument in method functions and the __init__ constructor method are the two cornerstones of OOP code in Python; if you get these, you should be able to read the text of most OOP Python code—apart from these, it's largely just packages of functions. The inheritance search matters too, of course, but self represents the automatic object argument, and __init__ is widespread.

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/88261182