classHouse():@propertydeftemperature(self):return self._temperature*10@temperature.setterdeftemperature(self,temp): self._temprature =some_func(temp)#use this ashouse =House()temp = house.temperaturehouse.temperature =50.0
@classmethod and @staticmethod
# Python program to demonstrate # use of a class method and static method. from datetime import date classPerson: def__init__(self,name,age): self.name = name self.age = age # a class method to create a # Person object by birth year. @classmethoddeffromBirthYear(cls,name,year): returncls(name, date.today().year - year)# a static method to check if a # Person is adult or not. @staticmethoddefisAdult(age): return age >18person1 =Person('mayank', 21)person2 = Person.fromBirthYear('mayank', 1996)print (person1.age)print (person2.age)# print the result print (Person.isAdult(22))
See how you can create class instance through some other logic using @classmethod.