Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

Python property Class

# using property class
class Celsius:
    def __init__(self, temperature=0):
        self.temperature = temperature
    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32
    # getter
    def get_temperature(self):
        print("Getting value...")
        return self._temperature
    # setter
    def set_temperature(self, value):
        print("Setting value...")
        if value < -273.15:
            raise ValueError("Temperature below -273.15 is not possible")
        self._temperature = value
    # creating a property object
    temperature = property(get_temperature, set_temperature)
Comment

PREVIOUS NEXT
Code Example
Python :: Get the first 4 numbers of the innermost arrays using numpy 
Python :: how print array in python with clean dublication 
Python :: boolien in python 
Python :: dft numpz phase 
Python :: minio python check if bucket exists 
Python :: nltk hide download messages 
Python :: every cell change comma to point pandas 
Python :: read data from gooogle cloud storage 
Python :: dockerize django app 
Python :: how to do merge sort in python 
Python :: python linux command 
Python :: python check if attribute exists in dictionary 
Python :: How To Let Your Main Window Appear after succesful login in Tkinter 
Python :: add output to setting scrapy 
Python :: pd dataframe 
Python :: How to Loop Through Sets in python 
Python :: jupyterthemes jplot 
Python :: request login python 
Python :: alexa python get slot value 
Python :: python remove last part of string 
Python :: Import "sendgrid" could not be resolved django 
Python :: seaborn bar plot sort for weekday 
Python :: scikit learn decistion tree 
Python :: pair plot seaborn 
Python :: inicio programacao python 
Python :: python string lower method 
Python :: mechanize python #3 
Python :: python export 16 bit tiff 
Python :: discord chatterbot python 
Python :: get object by name blender python 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =