Anatomie třídy: konstruktor __init__

Object-Oriented Programming in Python

Alex Yarosh

Content Quality Analyst @ DataCamp

Metody a atributy

  • Metody jsou definice funkcí uvnitř třídy
  • self jako první argument
  • Atributy definujte přiřazením
  • Na atributy třídy odkazujte přes self.___

   

class MyClass:
    # function definition in class
    # first argument is self
    def my_method1(self, other_args...):
        # do things here

    def my_method2(self, my_attr):        
        # attribute created by assignment
        self.my_attr = my_attr
        ...
Object-Oriented Programming in Python

Konstruktor

  • Chcete přidat data do objektu při jeho vytvoření?
  • Metoda konstruktoru __init__() je volána při každém vytvoření objektu.
class Customer:
       def __init__(self, name):     
        self.name = name           # <--- Create the .name attribute and set it to name parameter
        print("The __init__ method was called")

cust = Customer("Lara de Silva") #<--- __init__ is implicitly called print(cust.name)
The __init__ method was called
Lara de Silva
Object-Oriented Programming in Python
class Customer:
       def __init__(self, name, balance):  # <-- balance parameter added

self.name = name self.balance = balance # <-- balance attribute added print("The __init__ method was called")
cust = Customer("Lara de Silva", 1000) # <-- __init__ is called print(cust.name) print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Object-Oriented Programming in Python
class Customer:
       def __init__(self, name, balance=0):  #<--set default value for balance

self.name = name self.balance = balance print("The __init__ method was called")
cust = Customer("Lara de Silva") # <-- don't specify balance explicitly
print(cust.name) print(cust.balance) # <-- attribute is created anyway
The __init__ method was called
Lara de Silva
0
Object-Oriented Programming in Python

Atributy v metodách

class MyClass:
    def my_method1(self, attr1):
        self.attr1 = attr1
        ...

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...
obj = MyClass()
obj.my_method1(val1) # <-- attr1 created
obj.my_method2(val2) # <-- attr2 created

Atributy v konstruktoru

class MyClass:
    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2
        ...

# All attributes are created obj = MyClass(val1, val2)
  • snadnější přehled všech atributů
  • atributy jsou vytvořeny při vzniku objektu
  • přehlednější a udržitelnější kód
Object-Oriented Programming in Python

Osvědčené postupy

1. Inicializujte atributy v __init__()
Object-Oriented Programming in Python

Osvědčené postupy

1. Inicializujte atributy v __init__()
2. Pojmenování

CamelCase pro třídy, lower_snake_case pro funkce a atributy

Object-Oriented Programming in Python

Osvědčené postupy

1. Inicializujte atributy v __init__()
2. Pojmenování

CamelCase pro třídy, lower_snake_case pro funkce a atributy

3. Používejte self jako self
class MyClass:
    # This works but isn't recommended
    def my_method(kitty, attr):
       kitty.attr = attr
Object-Oriented Programming in Python

Osvědčené postupy

1. Inicializujte atributy v __init__()
2. Pojmenování

CamelCase pro třídy, lower_snake_case pro funkce a atributy

3. self je self
4. Používejte docstringy
class MyClass:
    """This class does nothing"""
    pass
Object-Oriented Programming in Python

Lass uns üben!

Object-Oriented Programming in Python

Preparing Video For Download...