पैकेज में क्लास जोड़ना

Python में Software Engineering Principles

Adam Spannbauer

Machine Learning Engineer at Eastman

ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग

OOP मॉड्यूलैरिटी

Python में Software Engineering Principles

क्लास की बनावट

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 में Software Engineering Principles

पैकेज में क्लास का उपयोग

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 में Software Engineering Principles

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 में Software Engineering Principles

अभ्यास करते हैं

Python में Software Engineering Principles

Preparing Video For Download...