Klassanatomi: konstruktorn __init__

Introduktion till objektorienterad programmering i Python

George Boorman

Curriculum Manager, DataCamp

Metoder och attribut

  • Metoder är funktionsdefinitioner i en klass
  • self som första argument
  • Definiera attribut via tilldelning
  • Referera till attribut i klassen via self.___
  • Att anropa många metoder kan bli ohållbart!
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 ...
Introduktion till objektorienterad programmering i Python

Konstruktor

  • Lägg till data i objektet när det skapas
  • Konstruktormetoden __init__() anropas varje gång ett objekt skapas
    • Anropas automatiskt tack vare syntaxen __metodnamn__
class Customer:
    def __init__(self, name):
        # Create the .name attribute and set it to name parameter
        self.name = name
        print("The __init__ method was called")
Introduktion till objektorienterad programmering i Python

Konstruktor

# __init__ is implicitly called 
cust = Customer("Lara de Silva")   
print(cust.name)
The __init__ method was called
Lara de Silva
Introduktion till objektorienterad programmering i Python

Attribut i metoder

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

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...

obj = MyClass() # attr1 created obj.my_method1(val1) # attr2 created obj.my_method2(val2)

Attribut i konstruktorn

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

# All attributes are created obj = MyClass(val1, val2)
  • Använd konstruktorn som huvudregel
  • Attribut skapas när objektet skapas
  • Mer användbar och underhållbar kod
Introduktion till objektorienterad programmering i Python

Lägg till argument

class Customer:
    # Add balance argument
    def __init__(self, name, balance): 

self.name = name # Add the balance attribute self.balance = balance print("The __init__ method was called")
Introduktion till objektorienterad programmering i Python

Lägg till parametrar

# __init__ is called
cust = Customer("Lara de Silva", 1000)
print(cust.name)
print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Introduktion till objektorienterad programmering i Python

Standardargument

class Customer:
    # Set a default value for balance
    def __init__(self, name, balance=0):
        self.name = name
        # Assign the new attribute
        self.balance = balance
        print("The __init__ method was called")
Introduktion till objektorienterad programmering i Python

Standardargument

# Don't specify the balance explicitly
cust = Customer("Lara de Silva")

print(cust.name) # The balance attribute is created anyway print(cust.balance)
The __init__ method was called
Lara de Silva
0
Introduktion till objektorienterad programmering i Python

Bästa praxis

1. Initiera attribut i __init__()
Introduktion till objektorienterad programmering i Python

Bästa praxis

1. Initiera attribut i __init__()
2. Namngivning

CamelCase för klasser, lower_snake_case för funktioner och attribut

Introduktion till objektorienterad programmering i Python

Bästa praxis

1. Initiera attribut i __init__()
2. Namngivning

CamelCase för klasser, lower_snake_case för funktioner och attribut

3. Behåll self som self
class MyClass:
    # This works but isn't recommended
    def my_method(george, attr):
        george.attr = attr
Introduktion till objektorienterad programmering i Python

Bästa praxis

1. Initiera attribut i __init__()
2. Namngivning

CamelCase för klasser, lower_snake_case för funktioner och attribut

3. self är self
4. Använd docstrings
class MyClass:
    """This class does nothing"""
    pass
Introduktion till objektorienterad programmering i Python

Nu kör vi en övning!

Introduktion till objektorienterad programmering i Python

Preparing Video For Download...