Bổ sung chức năng cho package

Nguyên tắc Kỹ thuật Phần mềm với Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Cấu trúc package

Cấu trúc package

Nguyên tắc Kỹ thuật Phần mềm với Python

Thêm chức năng

làm việc trong work_dir/my_package/utils.py

def we_need_to_talk(break_up=False):
    """Helper for communicating with significant other"""
    if break_up:
        print("It's not you, it's me...")
    else:
        print('I <3 U!')

làm việc trong work_dir/my_script.py

# Import utils submodule
import my_package.utils

# Decide to start seeing other people
my_package.utils.we_need_to_talk(break_up=True)
It's not you, it's me...
Nguyên tắc Kỹ thuật Phần mềm với Python

Import chức năng với __init__.py

làm việc trong work_dir/my_package/__init__.py

from .utils import we_need_to_talk

làm việc trong work_dir/my_script.py

# Import custom package
import my_package

# Realize you're with your soulmate
my_package.we_need_to_talk(break_up=False)
I <3 U!
Nguyên tắc Kỹ thuật Phần mềm với Python

Mở rộng cấu trúc package

Cấu trúc package mở rộng

Nguyên tắc Kỹ thuật Phần mềm với Python

Mở rộng cấu trúc package

Cấu trúc subpackage

Nguyên tắc Kỹ thuật Phần mềm với Python

Let's Practice

Nguyên tắc Kỹ thuật Phần mềm với Python

Preparing Video For Download...