Answers for "python property class attribute"

2

python property

class Number:

    def __init__(self, number):
        self.__number = number

    def get_number(self):
        print("Getting...")
        return self.__number

    def set_number(self, number):
        print("Setting...")
        self.__number = number
        
    def del_number(self):
        print("Deleting...")
        del self.__number

    nb = property()
    nb = nb.getter(get_number)
    nb = nb.setter(set_number)
    nb = nb.deleter(del_number)

n = Number(42)

print(n.nb)
n.nb = 69
print(n.nb)
del n.nb

# Getting...
# 42        
# Setting...
# Getting...
# 69
# Deleting...
Posted by: Guest on August-31-2021

Python Answers by Framework

Browse Popular Code Answers by Language