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

@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 "for in" loop to print the last item in the list 
Python :: copy a dictionary python 
Python :: how to write to a specific line in a file python 
Python :: beautifulsoup find element by partial text 
Python :: Filter with List Comprehension 
Python :: how to extract field values in list from queryset in django 
Python :: one liner if else replacement in python 
Python :: numpy random choice 
Python :: docker mount volume 
Python :: how to convert pandas series to 2d numpy array 
Python :: randint() 
Python :: RGB To Hex Conversion python 
Python :: tk inter entry 
Python :: print input in python 
Python :: set and tuple in python 
Python :: reset index in pandas 
Python :: multiline comment in python 
Python :: Using Python, getting the name of files in a zip archive 
Python :: python turn positive into negative 
Python :: pathlib path forward or back slahses 
Python :: pygame.events 
Python :: floating point python 
Python :: Python Date object to represent a date 
Python :: how to check if a variable in python is a specific data type 
Python :: python urlparse get domain 
Python :: how to change todays date formate in python 
Python :: lambda function dataframe 
Python :: python start process in background and get pid 
Python :: Create chatbot in Python - Source: NAYCode.com 
Python :: dynamic plot jupyter notebook 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =