Python高级编程

最近要进面试,要求中要会python,在工作中层使用过python,但并非主流语言,因此对其高级特性并不是很熟悉,因此再次机会详细学习其高级特性,包括面向对象的编程方法、一些特定的操作函数、常用的函数修饰符、异步语句等最新的python3语法。

Python的基础内容在Python核心编程(3.8学习笔记)_zYongheng的博客-CSDN博客

 一、继承和多态

1. 继承作用和多重继承的执行顺序

class Animal:
    def __init__(self, name):
        print("Animal Create")
        self.username = name

    def Say(self):
        pass

class Wise:
    def __init__(self):
        print("Wise Create")
        self.language = "Wise"

class Cat(Animal):
    def __init__(self, name):
        super().__init__(name)
        print("Cat Create")

    def Say(self):
        print("miaomiaomiao")

class People(Animal, Wise):
    def __init__(self):
        Wise.__init__(self)
        Animal.__init__(self, "People")
        # 使用super不能实现多继承
        # super(People, self).__init__("People")
        print("People Create")

if __name__ == '__main__':
    cat = Cat("name")
    cat.Say()
    print(isinstance(cat, Animal))
    print(isinstance(cat, Cat))

    people = People()

二、get、getattr、getitem、getattribute

class MyClass(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __getattribute__(self, item):
        # __getattribute是通过.的方式获取类的对象的方法,默认是支持的
        return super(MyClass, self).__getattribute__(item)

    def __getitem__(self, item):
        # __getitem__是通过[]的方式获取对象,默认是不支持的,但是可以重写该方法让其支持
        return super(MyClass, self).__getattribute__(item)

    def __getattr__(self, item):
        # __getattr__如果调用不存在的属性,会执行此函数,返回指定的结果
        return None


if __name__ == '__main__':
    c = MyClass("zhangsan", 18)
    print(c["name"])
    print(c.price)
class MyClass(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __get__(self, instance, owner):
        print("__get__")
        return "self"


class TestClass(object):
    c = MyClass("zhangsan", 18)

if __name__ == '__main__':
    testClass = TestClass()
    print(testClass.c)

三、dict.items() 和 dict.iteritems()

在python中items是返回整个数组,二iteritems()返回迭代器,但是从Python3.5中,items()直接返回迭代器而删除了iteritems的方法。

if __name__ == '__main__':
    d = {1: "one", 2: "two", 3: "three"}
    for k,v in d.items():
        print(k, v)

    # for k, v in d.iteritems():
    #     print(k, v)

四、range和xrang的区别

同样range是返回列表对象,而xrange返回是生成器,但是在Python3中,range代替了xrange,而取消了xrange的定义。

五、@classmethod和@staticmethod的区别

class A(object):
    count = 0

    def m1(self):
        # 该方法是实例方法
        A.count += 1
        self.count += 1
        print("self m1", self)

    @classmethod
    def m2(cls):
        # 该方法是类的方法
        cls.count += 1
        print("cls", cls)

    @staticmethod
    def m3():
        # 该方法和类没有关系
        print()

if __name__ == '__main__':
    a = A()
    a.m1()
    A.m2()
    a.m3()

六、迭代对象、迭代器、生成器
- 迭代对象是一组容器

- 迭代器是容器中的__iter__返回的的对象,不需要生成对象所有内容,它实现了__next__和__iter__方法

- 生成器是一种特殊的迭代器方法,使用yield返回结果

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值