Python学习笔记(二十八)- 编写类的细节(Class Coding Details)

1.什么是抽象超类(abstract superclass)?
答:抽象超类是一个调用方法的类,但不继承或定义它 - 它希望该方法由子类来填充。这通常用作在编写更具体的子类之前无法预测行为时概括类的方法。 OOP框架还将此作为一种调度到基于客户定义的可自定义操作的方式。

 

2.当一个简单的赋值语句出现在类语句的顶层时会发生什么?
答:.当一个简单赋值语句(X = Y)出现在类语句的顶层时,它会将一个数据属性附加到该类(Class.X)。与所有类属性一样,这将由所有实例共享;但是,数据属性不是可调用的方法函数。

 

3.为什么一个类需要在超类中手动调用__init__方法?
答:如果类定义了自己的__init__构造函数并且仍然希望运行超类的构造代码,则必须在超类中手动调用__init__方法。 Python本身只会自动运行一个构造函数 - 树中最低的构造函数。超类构造函数通常通过类名调用,手动传递自我实例:Superclass .__ init __(self, ...)。

 

4.如何加强而不是完全替换继承的方法?
答:要加强而不是完全替换继承的方法,请在子类中重新定义它,但是从子类中的方法的新版本手动回调该方法的超类的版本。也就是说,手动将self实例传递给方法的超类版本:Superclass.method(self,...)。

 

5.类的局部范围与函数的范围有何不同?
答:类是局部作用域,可以访问封闭的局部作用域,但它不能作为进一步嵌套代码的封闭局部作用域。与模块一样,类的局部范围在运行类语句后变为属性命名空间。

 

6.亚述的首都。。。是什么?
答:Ashur(或Qalat Sherqat),Calah(或Nimrud),短命的Dur Sharrukin(或Khorsabad),最后是Nineveh。

 

一些别的东东:

1.

class name(superclass, ...):         #把对象赋给name
    attr = value                     #共享的类数据
    def method(self, ...):           #方法(method)
        self.attr = value            #单个实例的数据

2.static method 静态函数 没有实例的函数

3.Module和Class

Module和Class都是命名空间

Modules:

(1)实现数据、逻辑包

(2)被Python文件或者其他语言扩展

(3)通过被导入来使用

(4)形成Python程序架构的顶层

Class:

(1)实现新的全功能对象

(2)用class语句创建

(3)通过被调用使用

(4)总是在模块中生存(live)

 

 

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

1. What is an abstract superclass?
2. What happens when a simple assignment statement appears at the top level of a class statement?
3. Why might a class need to manually call the __init__ method in a superclass?
4. How can you augment, instead of completely replacing, an inherited method?
5. How does a class's local scope differ from that of a function?
6. What...was the capital of Assyri

1. An abstract superclass is a class that calls a method, but does not inherit or define it—it expects the method to be filled in by a subclass. This is often used as a way
to generalize classes when behavior cannot be predicted until a more specific subclass is coded. OOP frameworks also use this as a way to dispatch to client-defined, customizable operations.
2. When a simple assignment statement (X = Y) appears at the top level of a class statement, it attaches a data attribute to the class (Class.X). Like all class attributes, this will be shared by all instances; data attributes are not callable method functions, though.
3. A class must manually call the __init__ method in a superclass if it defines an __init__ constructor of its own and still wants the superclass's construction code to run. Python itself automatically runs just one constructor—the lowest one in the tree. Superclass constructors are usually called through the class name, passing in the self instance manually: Superclass.__init__(self, ...).
4. To augment instead of completely replacing an inherited method, redefine it in a subclass, but call back to the superclass's version of the method manually from the new version of the method in the subclass. That is, pass the self instance to the superclass's version of the method manually: Superclass.method(self, ...).
5. A class is a local scope and has access to enclosing local scopes, but it does not serve as an enclosing local scope to further nested code. Like modules, the class local scope morphs into an attribute namespace after the class statement is run.
6. Ashur (or Qalat Sherqat), Calah (or Nimrud), the short-lived Dur Sharrukin (or Khorsabad), and finally Nineveh.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值