The Python Tutorial之Class(三)

1. Inheritance

  • The name BaseClassName must be defined in a scope containing the derived class definition. In place of a base class name, other arbitrary expressions are also allowed. This can be useful, for example, when the base class is defined in another module:class DerivedClassName(modname.BaseClassName):
  • Execution of a derived class definition proceeds the same as for a base class. When the class object is constructed, the base class is remembered. This is used for resolving attribute references: if a requested attribute is not found in the class, the search proceeds to look in the base class. This rule is applied recursively if the base class itself is derived from some other class.这种对属性的引用会顺着继承关系往下在基类寻找
  • There’s nothing special about instantiation of derived classes: DerivedClassName() creates a new instance of the class. Method references are resolved as follows: the corresponding class attribute is searched, descending down the chain of base classes if necessary, and the method reference is valid if this yields a function object.第二点是指class object说的,第三点是指instance of class说的
  • Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it.继承的子类很有可能覆盖基类的method,这点需要注意
  • An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments). This is occasionally useful to clients as well. (Note that this only works if the base class is accessible as BaseClassName in the global scope.)
  • Python has two built-in functions that work with inheritance:
  • Use isinstance() to check an instance’s type: isinstance(obj, int) will be True only if obj._class_ is int or some class derived from int.
  • Use issubclass() to check class inheritance: issubclass(bool, int) is True since bool is a subclass of int. However, issubclass(float, int) is False since float is not a subclass of int.

下面的例子可以完全说明上面的问题:

class base_a ():
    def put_b(self):
        print("A")
    def put_a(self):
        self.put_b()


class a(base_a):
    def put_b(self):
        print("B")
        base_a.put_b(self)



if __name__=='__main__':
    b=base_a()
    b.put_a()
    #---
    b=a()
    #基类的method被覆盖
    b.put_a()
    #判断是否是实例或者基类
    print(isinstance(b,base_a))
    print(issubclass(a,base_a))

结果为:

>>>
= RESTART: C:/Users/dhuang/AppData/Local/Programs/Python/Python36-32/test.py =
A
B
A
True
True
>>>

2. Multiple Inheritance

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>
  • Thus, if an attribute is not found in DerivedClassName, it is searched for in Base1, then (recursively) in the base classes of Base1, and if it was not found there, it was searched for in Base2, and so on.
  • In fact, it is slightly more complex than that; the method resolution order changes dynamically to support cooperative calls to super().关于动态解析的知识了解一下就行

针对第一点理解以下,做以下尝试:

class base_a():
    def put_b(self):
        print("A")
    def put_a(self):
        self.put_b()


class a(base_a):
    def put_e(self):
        print("B")
        base_a.put_b(self)


class b(a):
    def put_c(self):
        print("C")

if __name__=='__main__':
    c=b()
    c.put_b()

结果:

>>>
A
#c.put_b()沿着继承关系找到基类base_a中的put_b方法
>>>

3. Private Variables

  • it still is possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
  • Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.

针对以上两点进行实验,如下:

class base_a ():
    def __init__(self):
        self.__private=1
        self.public=2
    def put_pub(self):
        print(slef.public)
    def put_pri(self):
        print(self.__private)

if __name__=='__main__':
    #first test
    c=base_a()
    c.put_pri()
    print(c.__private)

看运行结果:

1  #无法访问实例私有变量
Traceback (most recent call last):
  File "C:\Users\dhuang\AppData\Local\Programs\Python\Python36-32\test.py", line 14, in <module>
    print(c.__private)
AttributeError: 'base_a' object has no attribute '__private'
>>>

进一步修改代码深入:

if __name__=='__main__':
    #first test
    c=base_a()
    c.put_pri()
#   print(c.__private)
    print(c._base_a__private)
#second test
    c._basea_private=2
    print(c._base_a__private)

结果:

>>>
= RESTART: C:\Users\dhuang\AppData\Local\Programs\Python\Python36-32\test.py =
1
1
1 #通过加上类名可以访问实例私有变量,但是无法改变其值
>>>

以上小代码为理解上述第一点内容,但是实际中的作用,还得以后的学习体会。然后对第二点内容进行理解:

class base_a ():
    def put_b(self):
        print("A")
    def put_a(self):
        self.__private_b()
    __private_b=put_b

class a(base_a):
    def put_b(self):
        print("B")
        base_a.put_b(self)



if __name__=='__main__':
    b=a()
    b.put_a()

结果如下:

>>>
= RESTART: C:\Users\dhuang\AppData\Local\Programs\Python\Python36-32\test.py =
A #通过类私有变量保存了基类的方法,避免被子类的方法覆盖
>>>

4. Iterators

官方的手册指出来下面几点:

  • for采用的实现是通过调用iter()返回一个iterator object,然后调用object.__next__去迭代,当超过index的范围就会rasie一个异常来终止for.
  • 我们自己可以的类可以仿照这样的例子来支持迭代特性。
  • 该自定义类要含有__iter()__,__next__()方法
class owniter:
    def __init__(self,a):
        self.data=a
        self.datalen=len(a)
        self.index=0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index == self.datalen :
            raise StopIteration
        self.index =self.index+1
        return self.data[self.index-1]

if __name__=='__main__':
    for i in owniter('qwm'):
        print i

结果:

>>>
= RESTART: C:\Users\dhuang\AppData\Local\Programs\Python\Python36-32\test.py =
q
w
m
>>>

7. Generators

  • Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed).
  • What makes generators so compact is that the _iter_() and next() methods are created automatically.
  • In addition to automatic method creation and saving program state, when generators terminate, they automatically raise StopIteration.

笔者认为只需要重点记住yeild用在我们想返回数据的地方,然后记住一个例子就可以了:

def  owniter(data):
    for i in range(0,len(data),1)
        yield data[i]


for i in owniter('qwm'):
    print(i)

结果:

q
w
m

8. Generator Expressions

These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expressions are more compact but less versatile than full generator definitions and tend to be more memory friendly than equivalent list comprehensions.Generator Expressions更加节省空间

直接看看官网给的例子,然后照葫芦画瓢的使用:

>>> sum(i*i for i in range(10))                 # sum of squares
285

>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec))         # dot product
260

>>> from math import pi, sin
>>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)}

>>> unique_words = set(word  for line in page  for word in line.split())

>>> valedictorian = max((student.gpa, student.name) for student in graduates)

>>> data = 'golf'
>>> list(data[i] for i in range(len(data)-1, -1, -1))
['f', 'l', 'o', 'g']

9. 总结

以上内容,还是需要在敲代码中逐渐掌握,官网的手册并没有很详细论述,有些地方依然是比较模糊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要爬取 https://www.runoob.com/manual/pythontutorial/docs/html/ 的一段文本,可以使用 Python 的 requests 库和 BeautifulSoup 库。 以下是一个示例代码,用于爬取 Python 教程第一篇文章的第一段内容: ```python import requests from bs4 import BeautifulSoup url = 'https://www.runoob.com/manual/pythontutorial/docs/html/python-beginner.html' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') content = soup.find('div', class_='article-intro').p.text.strip() print(content) ``` 首先,使用 requests 库获取 Python 教程的第一篇文章的页面内容,并将其存储在变量 response 。 接着,使用 BeautifulSoup 库解析页面内容,并使用 `soup.find()` 方法找到页面class 为 `article-intro` 的 div 元素,然后再从这个 div 元素找到第一个 p 标签元素。 最后,使用 `text` 属性获取 p 标签元素的文本内容,并使用 `strip()` 方法去除文本内容前后的空格和换行符。 执行上述代码,输出第一篇文章的第一段内容: ``` Python 是一种解释型、面向对象、动态数据类型的高级程序设计语言。Python 由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。Python 语法简洁而清晰,具有丰富和强大的类库。它常被称为胶水语言,能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起。Python 适用于大多数平台,包括 Windows、Linux、Unix、Mac OS X 等,并且有许多第方库可以帮助我们进行各种操作。 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值