Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

# Algorithms I.pptx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

## Consultez-les par la suite

1 sur 8 Publicité

# Algorithms I.pptx

Training

Publicité
Publicité

## Plus De Contenu Connexe

Publicité

### Algorithms I.pptx

1. 1. Week 3 Algorithms I NDG Training BAND A: 4:00 – 8:00pm
2. 2. 2 Overview  Getter & Setter in Python  Searching and sorting algorithms
3. 3. 3 Getter & Setter Getters and Setters are not the same as those in other object- oriented programming languages. Basically, the main purpose of using getters and setters in object-oriented programs is to ensure data encapsulation. What is Getter in Python? Getters are the methods that are used in Object-Oriented Programming (OOPS) to access a class's private attributes. The setattr() function in Python corresponds to the getattr() function in Python. What is Setter in Python? The setter is a method that is used to set the property's value. It is very useful in object-oriented programming to set the value of private attributes in a class.
4. 4. 4 Cont’d Using the normal function to achieve getters and setter behavior: There will be no special implementation if we specify normal get() and set() methods to achieve the getters & setters property. class MyClass: def __init__(self, age = 0): self._age = age # using the getter method def get_age(self): return self._age # using the setter method def set_age(self, a): self._age = a John = MyClass() #using the setter function John.set_age(19) # using the getter function print(John.get_age()) print(John._age)
5. 5. 5 Using the property() function to achieve getters and setter behavior property() is a built-in function for creating and returning a property object. There are three methods on a property object: getter(), setter(), and delete(). In Python, the property() function takes four arguments: property(fget, fset, fdel, doc). The fget function is used for retrieving the attribute value. The fset function is used for setting the value of an attribute. The fdel function is used to delete the value of an attribute. The attribute is given a docstring by doc.
6. 6. 6 Infinite Loop class MyClass: def __init__(self): self._age = 0 # using the get function def get_age(self): print("getter method") return self._age # using the set function def set_age(self, y): print("setter method") self._age = y # using the del function def del_age(self): del self._age age = property(get_age, set_age, del_age) John = MyClass() John.age = 18 print(John.age)
7. 7. 7 Numeric Data Type @property is one of Python's built-in decorators. The primary goal of any decorator is to modify the class methods or attributes so that the class user does not need to change their code. Example: class MyClass: def __init__(self): self._age = 0 # using the getter function @property def age(self): print("getter method") Using the @property decorators to achieve getters and setter behavior: return self._age #now, using the setter function @age.setter def age(self, x): if(x < 20): raise ValueError("the age is below eligibility criteria") print("setter method") self._age = x John = MyClass() John.age = 25 print(John.age)
8. 8. Congratulations  You can now save time coding by putting routine statements into Algorithms and Method (Accessor & Mutator)