Vlastnosti

Object-Oriented Programming in Python

Alex Yarosh

Content Quality Analyst @ DataCamp

Změna hodnot atributů

class Employee:
  def set_name(self, name):
    self.name = name 
  def set_salary(self, salary):
    self.salary = salary 
  def give_raise(self, amount):
    self.salary = self.salary + amount

def __init__(self, name, salary): self.name, self.salary = name, salary
emp = Employee("Miriam Azari", 35000)
# Use dot syntax and = to alter atributes
emp.salary = emp.salary + 5000
Object-Oriented Programming in Python

Změna hodnot atributů

class Employee:
  def set_name(self, name):
    self.name = name 
  def set_salary(self, salary):
    self.salary = salary 
  def give_raise(self, amount):
    self.salary = self.salary + amount

def __init__(self, name, salary): self.name, self.salary = name, salary
emp = Employee("Miriam Azari", 35000)
# Use dot syntax and = to alter atributes
emp.salary = emp.salary + 5000

 

 

Kontrola přístupu k atributům?

  • ověření platnosti hodnoty
  • nebo atributy jen pro čtení
    • úprava set_salary() by nezabránila emp.salary = -100
Object-Oriented Programming in Python

Omezené a pouze čitelné atributy

import pandas as pd
df = pd.DataFrame({"colA": [1,2], "colB":[3,4]})
df
  colA colB
0    1    3
1    2    4
df.columns = ["new_colA", "new_colB"]
df
  new_colA  new_colB
0    1    3
1    2    4
# will cause an error
df.columns = ["new_colA", "new_colB", "extra"]
df
ValueError: Length mismatch: 
Expected axis has 2 elements, 
new values have 3 elements
df.shape = (43, 27)
df
AttributeError: can't set attribute
Object-Oriented Programming in Python

@property

class Employer:
  def __init__(self, name, new_salary):
     self._salary = new_salary


@property def salary(self): return self._salary
@salary.setter def salary(self, new_salary):
if new_salary < 0: raise ValueError("Invalid salary") self._salary = new_salary

 

← Pro uložení dat použijte „chráněný" atribut s předponou _

← Použijte @property na metodu, jejíž název odpovídá přesně názvu omezeného atributu; vraťte interní atribut

← Použijte @attr.setter na metodu attr(), která bude volána při obj.attr = value

  • přiřazovaná hodnota je předána jako argument
Object-Oriented Programming in Python

@property

class Employer:
  def __init__(self, name, new_salary):
     self._salary = new_salary


@property def salary(self): return self._salary
@salary.setter def salary(self, new_salary): if new_salary < 0: raise ValueError("Invalid salary") self._salary = new_salary
emp = Employee("Miriam Azari", 35000)
# accessing the "property"
emp.salary
35000
emp.salary = 60000 # <-- @salary.setter
emp.salary = -1000
ValueError: Invalid salary
Object-Oriented Programming in Python

Proč používat @property?

  $$\text{\Large{Z pohledu uživatele: chování jako atributy}}$$

 

$$\text{\Large{Z pohledu vývojáře: kontrola přístupu}}$$

Object-Oriented Programming in Python

Další možnosti

→ Nepřidávejte @attr.setter

Vytvoření vlastnosti jen pro čtení

→ Přidejte @attr.getter

Použijte pro metodu volanou při načtení hodnoty vlastnosti

→ Přidejte @attr.deleter

Použijte pro metodu volanou při smazání vlastnosti pomocí del

Object-Oriented Programming in Python

Pojďme si to procvičit!

Object-Oriented Programming in Python

Preparing Video For Download...