54
loading...
This website collects cookies to deliver better user experience
Classmethods, staticmethods and property are examples of what are called descriptors. These are objects which implement the __get__ , __set__ or __delete__ methods.
**- what are decorators?
- classmethods
- staticmethods
- @property**
Decorators are functions (or classes) that provide enhanced functionality to the original function (or class) without the programmer having to modify their structure.
**get\_percent(25, 100)**
**25.0, D**
class A:
def instance_method(self):
return self
**@classmethod
def class\_method(cls):
return cls**
A.class_method()
Name: John Score: 25 Total : 100
Name: Jack Score: 60 Total : 100
Name: Jill Score: 125 Total : 200
class A:
def instance_method(self):
return self
@classmethod
def class_method(cls):
return cls
**@staticmethod
def static\_method():
return**
a = A()
a.static_method()
A.static_method()
In python, there is no private keyword. We prepend a variable by a dunder(__ ) to show that it is private and shouldn’t be accessed or modified directly.
**print("Score: " + str(student1.score))**
**student1.score = 100**
**print(student1.get\_score())
student1.set\_score(100)**
Traceback (most recent call last):
File "main.py", line 16, in <module>
student.score = 10
**AttributeError: can't set attribute**
student = Student("Tom", 50, 100)
del student.score
This gives:
Traceback (most recent call last):
File "<string>", line 17, in <module>
**AttributeError: can't delete attribute**
Traceback (most recent call last):
File "<string>", line 23, in <module>
File "<string>", line 9, in x
**AttributeError: 'PythonClass' object has no attribute '\_\_score'**
student = Student("Bob", 350, 500)
**print(student.name)**
**Student Name: Bob**
student = Student("Bob", 350, 500)
print(student.score)
**student.score = 400** print(student.score)
70.0 %
**INFO:root:Setting new value...**
80.0 %