python基础6 类/库的引用

现实中,我们通常会将类定义在不同的python文件中,于是就涉及到不同python文件之间的调用。

同一目录下的python类之间调用

接之前的例子,目录下分别有class_slots.py和derived2.py,其代码分别如下:

class_slots.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#同目录下文件应用
from derived2 import Derived

class Car(object):
    __slots__ = ('_color', '_number')

    @property
    def color(self):
        return self._color
    
    @color.setter
    def color(self,value):
        self._color = value

    @property
    def number(self):
        return self._number
    
    @number.setter
    def number(self,value):
        self._number = value
    
    @number.deleter
    def number(self):
        print('oops! number is deleted!')
    
    def func(self):
        print('car number: %d' %  self._number)
    pass

if __name__ == "__main__":
    car = Car()
    car.color = "red"
    print(car.color)
    car.color = "yellow"
    print(car.color)
    car.number = 25
    car.func()
    del car.number

    derived = Derived()
    derived.derived_func()

derived2.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#缺省从object类继承的写法
class Base1:
    def __init__(self):
        print('Base1 init')

    def base1_func(self):
        print("base1_func")

    def same_func(self):
        print("base1_same_func")

class Base2:
    def __init__(self):
        print('Base2 init')

    def base2_func(self):
        print("base2_func")

    def same_func(self):
        print("base2_same_func")

class Derived(Base1,Base2):
    def __init__(self):
        #默认调用Base1类的init方法
        super().__init__()
        #调用Base1类的init方法
        Base1.__init__(self)  
        #调用Base2类的init方法
        Base2.__init__(self)  
        print('Derived init')

    def derived_func(self):
        print("derived_func")
    
    def same_func(self):
        #默认调用Base1类的same_func方法
        super().same_func()
        #调用Base1类的same_func方法
        Base1.same_func(self)  
        #调用Base2类的same_func方法
        Base2.same_func(self)  
        print('Derived same_func')

if __name__ == "__main__":
    derived = Derived()
    derived.base1_func()
    derived.base2_func()
    derived.derived_func()

    #根据MRO顺序,解析到继承的第一个类方法Base1
    derived.same_func()

    print(isinstance(derived,Base1))

结合上述两个文件代码可以发现,在class_slots.py中实现了对同级目录下derived2.py的调用。

以class_slots.py作为顶层执行("__main__"条件会判断当前是否作为顶层文件执行),输出如下:

red
yellow
car number: 25
oops! number is deleted!
Base1 init
Base1 init
Base2 init
Derived init
derived_func

根据输出可以看到,在class_slots.py中成功实现了对derived2.py中Derived2类的实例化和调用。

子目录下的python类之间调用

  1. 在class_slots.py所在目录中新建lib文件夹
  2. 在lib文件夹中新建一个空的__init__.py文件。创建该文件的目的是将test目录变成一个Python包;将derived2.py拷贝至lib文件夹内。
  3. 修改class_slots.py中的引用方式
from lib.derived2 import Derived

执行class_slots.py,可以得到和同目录引用一样的结果输出:

red
yellow
car number: 25
oops! number is deleted!
Base1 init
Base1 init
Base2 init
Derived init
derived_func

任意目录下的python文件引用

在 Python 中,sys.path 是一个包含解释器搜索模块路径的列表。通过使用 sys.path.append() 方法,可以动态地向该列表添加新的搜索路径。

#derived2.py在D:\python_study\lib目录下

import sys
sys.path.append(r"D:\python_study\lib")

from derived2 import Derived

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值