Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python property

class Car():
    def __init__(self):
        self._seat = None

    @property # getter
    def seat(self):
        return self._seat

    @seat.setter # setter
    def seat(self, seat):
        self._seat = seat


c = Car()
c.seat = 6
print(c.seat)
Comment

Python property

# Python @property decorator
class Foo:
    def __init__(self, my_word):
        self._word = my_word
    @property
    def word(self):
        return self._word
# word() is now a property instead of a method
print(Foo('ok').word)     # ok

class Bar:
    def __init__(self, my_word):
        self._word = my_word
    def word(self):
        return self._word
print(Bar('ok').word())   # ok   # word() is a method
Comment

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

@property python

# 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)


human = Celsius(37)

print(human.temperature)

print(human.to_fahrenheit())

human.temperature = -300
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 :: Python match.span() 
Python :: numpy fancy indexing 
Python :: adding new character in string python 
Python :: pandas replace not working 
Python :: Pandas index column title or name 
Python :: how to use query in ms access with python 
Python :: json to csv python github 
Python :: sklearn standardscaler for numerical columns 
Python :: Python send sms curl 
Python :: python as integer ratio 
Python :: accessing a specific slide using python 
Python :: pypy stragger 
Python :: ridge regression alpha values with cross validation score plot 
Python :: django python get more commands paramaters 
Python :: How to make colors.winapp in WindowsAP 
Python :: django chain query 
Python :: Odoo Module ACL(Access Controls List) 
Python :: how to print continuesly in the same line in python 
Python :: python cheat sheets 
Python :: read sharepoint list using python 
Python :: How to Move and Delete Files in Python 
Python :: python matrices access column 
Python :: python multiprocessing imap tqdm 
Python :: how to return value in new record to odoo 
Python :: python sum whole matrix comand 
Python :: multi line cooment in python 
Python :: getting input from button python 
Python :: python keyborad back space 
Python :: remove all the valu ein dict exacpt provided key pythn 
Python :: concatenate dataframes using one column 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =