Answers for "python class @property"

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
1

class attributes in python

class MyClass:
  # Class attributes are defined outside of constructor
  class_attr = 0
  
  def __init__(self, inst):
    # Instance attributes are defined in the constructor
    self.instance_attr = inst
    
obj = MyClass(1)
print(obj.class_attr) # outputs 0
print(obj.instance_attr) # outputs 1
print(MyClass.class_attr) # outputs 0
print(MyClass.instance_attr) # raises AttributeError
Posted by: Guest on May-30-2021
-1

@property in python

Getting name
The name is: Adam
Setting name to John
Deleting name
Posted by: Guest on December-07-2020

Code answers related to "python class @property"

Python Answers by Framework

Browse Popular Code Answers by Language