Anatomia klasy: konstruktor __init__

Programowanie obiektowe w Pythonie

Alex Yarosh

Content Quality Analyst @ DataCamp

Metody i atrybuty

  • Metody to definicje funkcji wewnątrz klasy
  • self jako pierwszy argument
  • Atrybuty definiuje się przez przypisanie
  • Odwołanie do atrybutów klasy przez 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
        ...
Programowanie obiektowe w Pythonie

Konstruktor

  • Jak dodać dane do obiektu podczas jego tworzenia?
  • Metoda konstruktora __init__() jest wywoływana przy każdym tworzeniu obiektu.
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
Programowanie obiektowe w Pythonie
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
Programowanie obiektowe w Pythonie
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
Programowanie obiektowe w Pythonie

Atrybuty w metodach

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

Atrybuty w konstruktorze

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

# All attributes are created obj = MyClass(val1, val2)
  • łatwiej poznać wszystkie atrybuty
  • atrybuty tworzone są przy tworzeniu obiektu
  • bardziej czytelny i łatwy w utrzymaniu kod
Programowanie obiektowe w Pythonie

Dobre praktyki

1. Inicjalizacja atrybutów w __init__()
Programowanie obiektowe w Pythonie

Dobre praktyki

1. Inicjalizacja atrybutów w __init__()
2. Nazewnictwo

CamelCase dla klas, lower_snake_case dla funkcji i atrybutów

Programowanie obiektowe w Pythonie

Dobre praktyki

1. Inicjalizacja atrybutów w __init__()
2. Nazewnictwo

CamelCase dla klas, lower_snake_case dla funkcji i atrybutów

3. self pozostaje self
class MyClass:
    # This works but isn't recommended
    def my_method(kitty, attr):
       kitty.attr = attr
Programowanie obiektowe w Pythonie

Dobre praktyki

1. Inicjalizacja atrybutów w __init__()
2. Nazewnictwo

CamelCase dla klas, lower_snake_case dla funkcji i atrybutów

3. self jest self
4. Używaj docstringów
class MyClass:
    """This class does nothing"""
    pass
Programowanie obiektowe w Pythonie

Czas na ćwiczenia!

Programowanie obiektowe w Pythonie

Preparing Video For Download...