Created
April 1, 2021 12:38
-
-
Save shashwata27/b8d284fd2e531b2e899333f033bcf2bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#decorators take a function as input and enhances it and returns the function | |
# function within a function which adds some features to the function and returns it | |
def decor(num): | |
def inner(): | |
print("enhanced") | |
num() | |
return inner | |
def num1(): | |
print("this is num1") | |
nan=decor(num1)#nan have the returned inner() func of the decor function | |
nan()# so we again have to call enhanced num1 through inner, and its instance is nan///ERROR | |
print() | |
@decor#this line replaces the above 2 lines now we can call it just by num2() | |
def num2(): | |
print("this is num2") | |
num2() | |
#some of the builtin decorators are @classmethod @staticmethod or @abstracmethod from abc | |
class student: | |
school="walter"#class variable | |
def __init__(self,roll,age): | |
self.rol=roll | |
self.ag=age | |
def get_rol(self): | |
return self.rol | |
def get_ag(self):#getter | |
return self.ag | |
def set_rol(self):#setter | |
self.rol=0 | |
def set_ag(self): | |
self.ag=5 | |
#def __get__(self, instance, owner): | |
# self.ag=instance | |
# student=owner | |
@classmethod# using decorator | |
def sch(cls): | |
return cls.school#class method | |
def scho(student):#class method without decorator | |
return student.school | |
@staticmethod#decorator | |
def info():#static method can access class variables | |
print("learning OOP", student.school) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment