Data instance a třídy

Object-Oriented Programming in Python

Alex Yarosh

Content Quality Analyst @ DataCamp

Základní principy OOP

 

Dědičnost:

  • Rozšíření funkcionality existujícího kódu

Polymorfismus:

  • Vytvoření jednotného rozhraní

Zapouzdření:

  • Sdružení dat a metod
Object-Oriented Programming in Python

Data na úrovni instance

class Employee:
    def __init__(self, name, salary):
       self.name = name
       self.salary = salary

emp1 = Employee("Teo Mille", 50000) 
emp2 = Employee("Marta Popov", 65000)
  • name, salary jsou atributy instance
  • self odkazuje na instanci
Object-Oriented Programming in Python

Data na úrovni třídy

  • Data sdílená všemi instancemi třídy
  • Atributy třídy se definují v těle class
class MyClass:
    # Define a class attribute
    CLASS_ATTR_NAME = attr_value


  • „Globální proměnná" v rámci třídy
Object-Oriented Programming in Python

Data na úrovni třídy

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    #<--- no self. 

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
  • MIN_SALARY je sdílena všemi instancemi
  • Pro definici atributu třídy nepoužívejte self
  • Pro přístup k atributu třídy použijte ClassName.ATTR_NAME
Object-Oriented Programming in Python

Data na úrovni třídy

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
emp1 = Employee("TBD", 40000)
print(emp1.MIN_SALARY)
30000
emp2 = Employee("TBD", 60000)
print(emp2.MIN_SALARY)
30000
Object-Oriented Programming in Python

Proč používat atributy třídy?

Globální konstanty vztahující se ke třídě

 

  • minimální/maximální hodnoty atributů
  • běžně používané hodnoty a konstanty, např. pi pro třídu Circle
  • ...
Object-Oriented Programming in Python

Metody třídy

  • Metody jsou již „sdílené": stejný kód pro každou instanci
  • Metody třídy nemohou používat data na úrovni instance
class MyClass:

  @classmethod                         # <---use decorator to declare a class method

def my_awesome_method(cls, args...): # <---cls argument refers to the class # Do stuff here # Can't use any instance attributes :(
MyClass.my_awesome_method(args...)
Object-Oriented Programming in Python

Alternativní konstruktory

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY
  • Lze mít pouze jeden __init__()

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)

  • Metody třídy slouží k vytváření objektů
  • Pro vrácení objektu použijte return
  • cls(...) zavolá __init__(...)
Object-Oriented Programming in Python

Alternativní konstruktory

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)        

textový soubor obsahující jeden řádek se jménem zaměstnance

# Create an employee without calling Employee()
emp = Employee.from_file("employee_data.txt")
type(emp)
__main__.Employee
Object-Oriented Programming in Python

Pojďme si procvičit!

Object-Oriented Programming in Python

Preparing Video For Download...