python高级编程第一讲:深入类和对象

1.鸭子类型和多态

多态的概念是应用于Java和C#这一类强类型语言中,而Python崇尚"鸭子类型"

所谓多态:定义时的类型和运行时的类型不一样,此时就成为多态。可能大家不太好理解,就是定义的时候我不知道调用谁,只有运行的时候才知道调用谁
我们先看一段代码:

class Cat(object):
#定义cat类型
    def info(self):
        print("I am Cat")
    

class Dog(object):
    #定义dog类
    def info(self):
        print("I am Dog")
        
class Duck(object):
    #定义duck类
    def info(self):
        print("I am Duck")
        
#将这些类放进一个列表中
animal_list = [Cat,Dog,Duck]

#遍历列表,我们的想法是此时这样调用应该是可以直接调用相应类里的info方法
for animal in animal_list:
    animal.info()

执行结果:
Traceback (most recent call last):
File “”, line 1, in
File “D:\PyCharm 2019.1\helpers\pydev_pydev_bundle\pydev_umd.py”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File “D:\PyCharm 2019.1\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “G:/pythonlianxi/spython/logic/python高级/第一讲/demo1.py”, line 23, in
animal.info()
TypeError: info() missing 1 required positional argument: ‘self’

以上代码中我们的想法是通过这种方法我们可以直接输出我们想要的结果,但是却忽略了一个很重要的点,就是我们根据平时类的实例化,在上述代码中,我们没有对类进行实例化所以就出来了错误
正确的代码应该是下面这种写法:

class Cat(object):
#定义cat类型
    def info(self):
        print("I am Cat")
    

class Dog(object):
    #定义dog类
    def info(self):
        print("I am Dog")
        
class Duck(object):
    #定义duck类
    def info(self):
        print("I am Duck")
        
#将这些类放进一个列表中
animal_list = [Cat,Dog,Duck]

#遍历列表,我们的想法是此时这样调用应该是可以直接调用相应类里的info方法
for animal in animal_list:
    #将类实例化,通过类名+()的这种形式
    animal().info()

执行结果:
I am Cat
I am Dog
I am Duck
此时我们可以知道如果要调用类的方法,先要将类进行实例化

2.抽象基类(abc模块)

  • 抽象基类(abstract base class,ABC):抽象基类就是类里定义了纯虚成员函数的类。纯虚函数只提供了接口,并没有具体实现。抽象基类不能被实例化(不能创建对象),通常是作为基类供子类继承,子类中重写虚函数,实现具体的接口。
  • 抽象基类就是定义各种方法而不做具体实现的类,任何继承自抽象基类的类必须实现这些方法,否则无法实例化。

2.1抽象基类应用场景

  • 1.我们去检查某个类是否有某种方法
    示例代码:
#检查某个类是否有某种方法
class Demo(object):
    def __init__(self,my_list):
        self.my_list = my_list
    def __len__(self):
        return len(self.my_list)  
d=Demo(['hello','world'])

from collections.abc import Sized
print(isinstance(d,Sized))

执行结果为:True
Sized是隐藏文件中实现的,其中有实现 __len()__方法,所以我们在最后判断的时候才会是True

  • 2.我们需要强制某个子类必须实现某些方法
  • 1此处我们模拟强制实现的场景:
    代码1:
#需要某个子类必须实现的方法
#此处为模拟实现
class CacheBase(object):
    def get(self,key):
        raise ValueError
    
    def set(self,key,value):
        raise InterruptedError
   
class RedisCache(CacheBase):
    pass
r = RedisCache()
r.get('zjk')

执行结果:此时程序会直接抛出异常,因为在实例化时没重写父类中的方法

我们重新对程序进行调整,代码如下:
代码2:

class CacheBase(object):
    def get(self,key):
        raise ValueError
    
    def set(self,key,value):
        raise InterruptedError
   
class RedisCache(CacheBase):
    def get(self,key):
        pass

r = RedisCache()
r.get('zjk')

此时程序正常运行,不报错,但是由于是模拟的,所以不准确,不建议用这种方法

  • 2 正确的写法

代码1没有在实例化对get和set方法重写,如下:

import abc
class CacheBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def get(self,key):
        pass
    
    @abc.abstractmethod
    def set(self,key,value):
        pass
   
class RedisCache(CacheBase):
    pass

r = RedisCache()
r.get('zjk')

此时执行程序,会报错,说没有相应的get和set方法

所以我们还要在实例中对相应的方法时行 重写
代码如下:

import abc
class CacheBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def get(self,key):
        pass
    
    @abc.abstractmethod
    def set(self,key,value):
        pass
    
class RedisCache(CacheBase):
    def get(self,key):
        pass
    def set(self,key,value):
        pass

r = RedisCache()
r.get('zjk')

3.isinstance和type的区别

常规的区别
    1. .isinstance函数来判断一个对象是否是一个已知的类型
s='123'
print(isinstance(s,str))

执行结果为:True

  • 2.type是直接告诉我们类型
s='123'
print(type(s))

执行结果为:<class ‘str’>

面向对象中的区别
  • type不考虑继承关系
  • instance 考虑继承关系

在面向对象编程中,要使用isinstance,避免误判

is 引用是否是同个对象 == 只判断数值是否相等

4.类属性和实例属性

先看下面的代码:

class A:
#类属性    
bb = 1
    
    def __init__(self,x,y):
        #实例属性
        self.x= x
        self.y= y
        
a = A(1,2)
A.bb=11
a.bb=22  #此句的实际作用相当于在实例里增加了一个bb属性,和类的bb属性不是一样的
print(a.x,a.y,a.bb)

print(A.bb)

print(A.x)

分析输出结果我们得出:实例可以向上查找类的属性,但是类不能向下查找属性的属性

5.查找顺序

mro 算法 在Python2.3之后,Python采用了C3算法
DFS

BFS

6.Python对象的自省机制

自省是通过一定的机制查询到对象的内部结构。
自省就是面向对象的语言所写的程序在运行时,能够知道对象的类型。简单一句就是,运行时能够获知对象的类型。

如:dir 函数 会列出相应的魔法方法和属性 ,type ,isinstance ,hasattri

class Person:
    name = 'zjk'
    
class Student(Person):
    def __init__(self, name):
        self.name = name
        
if __name__ == '__main__':
    u = Student('zs')
    print(u.__dict__)
    print(u.name)
    
    u.__dict__['add']= 'HZ'
    print(u.__dict__)
    print(dir())

7.super函数

为什么要用super函数?

  • 因为正常的调用父类中的方法,我们也可以通过 类名+方法名这种硬编码这种方式,一旦父类的名字发生改变,那么我们所有引用到地方全部要进行修改,代码维护性较差。而且通过父类名+方法名这种方法,在调用其方法时,传参的时候需要再传入一个self参数。
  • 而通过super().方法名 这种软编码的方法引用父类,如果父类名称发生变化,我们后面的代码不需要进行更新,因为super()会自动解析父类的信息。
  • super()在复杂的继承关系中,不是调用父类中方法,而是按照mro算法来进行调用 的
  • 想在实例方法中调用父类的方法
class A:
    def __init__(self):
          print("A")
class B(A):
      def __init__(self):
      print('B')
      super().__init__()
if __name__ == "__main__":
b = B()
  • 可以在实例方法中通过调用父类中已有的init初始化方法给新的类进行初始化的快速赋值
class Person(object):
    def __init__(self, name, age, heigt):
        self.name = name
        self.age = age
        self.height = heigt
    
    def speak(self):
        print('{}说:我{}岁'.format(self.name,self.age))
    
class Student(Person):
    def __init__(self,name,age,height,grade):
        super().__init__(name,age,height)
        self.grade = grade
    def info(self):
        print('{}说:我{}岁,我在{}年级'.format(self.name,self.age,self.grade))
        
if __name__ == '__main__':
    u = Student('zs',18,116,5)
    u.info()

8.类与对象尝试问题与解决技巧

  • 1 如何派生内置不可变类型修改其实例化行为?

场景:想自定义一种新类型的无组,对于传入的可迭代的对象,我们只保留其中int类型且值大于0的元素
IntTuple([2,-2,‘zs’,[‘x’,‘y’,4]])=>(2,4)
如何继承内置tuple实现inttuple

'''
想自定义一种新类型的无组,对于传入的可迭代的对象,我们只保留其中int类型且值大于0的元素
IntTuple([2,-2,'zs',['x','y',4]])=>(2,4)
如何继承内置tuple实现inttuple
'''

#定义IntTuple类
class IntTuple(tuple):
    #定义初始化方法,并传入 可迭代的参数
    def __init__(self,iterable):
        f = (i for i in iterable if isinstance(i,int) and i>0)
        super().__init__(f)
        
        
result = IntTuple([2,-2,'zs',['x','y',4]])
print(result)

执行结果如下:
Traceback (most recent call last):
File “”, line 1, in
File “D:\PyCharm 2019.1\helpers\pydev_pydev_bundle\pydev_umd.py”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File “D:\PyCharm 2019.1\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “G:/pythonlianxi/spython/logic/python高级/第一讲/demo6.py”, line 15, in
result = IntTuple([2,-2,‘zs’,[‘x’,‘y’,4]])
File “G:/pythonlianxi/spython/logic/python高级/第一讲/demo6.py”, line 12, in init
super().init(f)
TypeError: object.init() takes no parameters
我们可以看到程序报错了

下面我们通过几组代码来分析一下数据类型的一些初始化方法前还有哪些方法可用
第一段代码:

class A:
    def __new__(cls, *args, **kwargs):
        print("A__new__",cls,args)
        return object.__new__(cls) #new方法需要返回
    def __init__(self,*args):
        print("A__init__",args)
        
a=A(1,2)

执行结果:
A__new__ <class ‘main.A’> (1, 2)
A__init__ (1, 2)
我们可以看到在__init__前还有一个new方法

我们将代码再变形一下

class A:
    def __new__(cls, *args, **kwargs):
        print("A__new__",cls,args)
        return object.__new__(cls) #new方法需要返回
    def __init__(self,*args):
        print("A__init__",args)
        
#A.__new__(1,2)  #直接这样传会报错,因为在上面的代码中我们对类进行实例化,所以上面的代码不会报错,而此片因为没有对类进行实例化,所以 我们要遵守上面代码中一些规则,按规则传入相应的参数,所以要改为下面的写法
A.__new__(A,1,2)  #此时代码正常运行
A.__init__(A,1,2)

下面我们来看一下列表先执行的是哪个魔法方法:
代码:

ilist = list('abc')
print(ilist)
l = list.__new__(list,'abc')
print(l)

执行结果:
[‘a’, ‘b’, ‘c’]
[]

ilist = list('abc')
print(ilist)
l = list.__new__(list,'abc')
list.__init__(l,'abc')
print(l)

执行结果:
[‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’]

我们通过上面的2个关于list生成的代码,可以看到列表的初始化,不是通过new方法来实现的,而是通过 init来完成

我们再来看一下元组的

ituple =tuple('abc')
print(ituple)
newtuple = tuple.__new__(tuple,'bcd')
print(newtuple)

执行结果:
(‘a’, ‘b’, ‘c’)
(‘b’, ‘c’, ‘d’)
我们看到可以直接输出,由此可知,元组是通过new来完成,不是通过init来实现的。

所以我们将最开始的代码修改如下:

# #定义IntTuple类
class IntTuple(tuple):
    #定义初始化方法,并传入 可迭代的参数
    def __new__(cls,iterable):
        f = (i for i in iterable if isinstance(i,int) and i>0)
        return super().__new__(cls,f)


result = IntTuple([2,-2,'zs',['x','y'],4])
print(result)

此时已经完成了我们想要的功能!!

最后谢谢大家的阅读!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件: import scrapy from demo1.items import Demo1Item import urllib from scrapy import log # BOSS直聘网站爬虫职位 class DemoSpider(scrapy.Spider): # 爬虫名, 启动爬虫时需要的参数*必填 name = 'demo' # 爬取域范围,允许爬虫在这个域名下进行爬取(可选) allowed_domains = ['zhipin.com'] # 爬虫需要的url start_urls = ['https://www.zhipin.com/c101280600/h_101280600/?query=测试'] def parse(self, response): node_list = response.xpath("//div[@class='job-primary']") # 用来存储所有的item字段 # items = [] for node in node_list: item = Demo1Item() # extract() 将xpath对象转换为Unicode字符串 href = node.xpath("./div[@class='info-primary']//a/@href").extract() job_title = node.xpath("./div[@class='info-primary']//a/div[@class='job-title']/text()").extract() salary = node.xpath("./div[@class='info-primary']//a/span/text()").extract() working_place = node.xpath("./div[@class='info-primary']/p/text()").extract() company_name = node.xpath("./div[@class='info-company']//a/text()").extract() item['href'] = href[0] item['job_title'] = job_title[0] item['sa 报错: C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\python.exe "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\pydevconsole.py" --mode=client --port=55825 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\\Users\\xieqianyun\\demo1', 'C:/Users/xieqianyun/demo1']) Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.10.0 -- An enhanced Interactive Python. Type '?' for help. PyDev console: using IPython 7.10.0 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 runfile('C:/Users/xieqianyun/demo1/demo1/begin.py', wdir='C:/Users/xieqianyun/demo1/demo1') Traceback (most recent call last): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-fc5979762143>", line 1, in <module> runfile('C:/Users/xieqianyun/demo1/demo1/begin.py', wdir='C:/Users/xieqianyun/demo1/demo1') File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/xieqianyun/demo1/demo1/begin.py", line 3, in <module> cmdline.execute('scrapy crawl demo'.split()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\cmdline.py", line 145, in execute cmd.crawler_process = CrawlerProcess(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 267, in __init__ super(CrawlerProcess, self).__init__(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 145, in __init__ self.spider_loader = _get_spider_loader(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 347, in _get_spider_loader return loader_cls.from_settings(settings.frozencopy()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 61, in from_settings return cls(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 25, in __init__ self._load_all_spiders() File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 47, in _load_all_spiders for module in walk_modules(name): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\utils\misc.py", line 73, in walk_modules submod = import_module(fullpath) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:\Users\xieqianyun\demo1\demo1\spiders\demo.py", line 4, in <module> from scrapy import log ImportError: cannot import name 'log'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值