การเพิ่มคลาสลงใน Package

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Adam Spannbauer

Machine Learning Engineer at Eastman

การเขียนโปรแกรมเชิงวัตถุ

ความเป็นโมดูลของ OOP

หลักการวิศวกรรมซอฟต์แวร์ใน Python

โครงสร้างของคลาส

ทำงานใน work_dir/my_package/my_class.py

# Define a minimal class with an attribute
class MyClass:

"""A minimal example class :param value: value to set as the ``attribute`` attribute :ivar attribute: contains the contents of ``value`` passed in init """
# Method to create a new instance of MyClass def __init__(self, value): # Define attribute with the contents of the value param self.attribute = value
หลักการวิศวกรรมซอฟต์แวร์ใน Python

การใช้คลาสใน Package

ทำงานใน work_dir/my_package/__init__.py

from .my_class import MyClass

ทำงานใน work_dir/my_script.py

import my_package

# Create instance of MyClass
my_instance = my_package.MyClass(value='class attribute value')

# Print out class attribute value
print(my_instance.attribute)
'class attribute value'
หลักการวิศวกรรมซอฟต์แวร์ใน Python

Convention ของ self

ทำงานใน work_dir/my_package/my_class.py

# Define a minimal class with an attribute
class MyClass:
    """A minimal example class

    :param value: value to set as the ``attribute`` attribute
    :ivar attribute: contains the contents of ``value`` passed in init
    """

    # Method to create a new instance of MyClass
    def __init__(self, value):
        # Define attribute with the contents of the value param
        self.attribute = value
my_instance = my_package.MyClass(value='class attribute value')
หลักการวิศวกรรมซอฟต์แวร์ใน Python

มาฝึกกันเถอะ!

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Preparing Video For Download...