1. 程式人生 > >Python內置函數之classmetho staticmethod

Python內置函數之classmetho staticmethod

對象 sta disco prop apple 函數 new discount count

當對類的靜態屬性進行修改時(不需要借助於對象就行類靜態屬性的修改)

class Goods:
    discount = 0.5
    def __init__(self,name,price):
        self.name = name
        self.__price = price

    @property
    def price(self):
        return  self.__price * Goods.discount
    @classmethod 
    def changeDiscount(cls,new_discount):
        cls.discount 
= new_discount apple = Goods(apple,10) Goods.changeDiscount(0.1) print(apple.price)

Python內置函數之classmetho staticmethod