Python_类和装饰器

class Student(object):
    def __init__(self,name,grade):
        self.name=name
        self.grade=grade

    def introduce(self):
        print("hello,i'm ", self.name,end="")
        print(", my grade is ",self.grade,"!")
        print("-----------------------")

    def change(self,num):
        self.grade = self.grade + num

jim=Student("jim",50)
jim.introduce()
jim.change(10)
jim.introduce()
 
 

Python_类和装饰器_第1张图片

#装饰器
#函数可以直接做函数形参 ,可以作为函数返回值
def add_candles(cake_func):
    def insert_candles():
        return cake_func() + ' and candles'
    return insert_candles
@add_candles
def make_cake():
    return 'cake'
print(make_cake())



你可能感兴趣的:(Python_类和装饰器)