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 check if list contains 
Python :: python file hashlib 
Python :: pygame get keypress code 
Python :: turn list in to word python 
Python :: where is python installed on ubuntu 
Python :: how to update sklearn 
Python :: check if element in list python 
Python :: create a dataframe from dict 
Python :: Python Roman to Integer method 2 
Python :: install aws sdk python 
Python :: reading json file 
Python :: tkinter filedialog get directory path 
Python :: django q objects 
Python :: how to reverse a string in python 
Python :: word2number python 
Python :: python offline translate pypi 
Python :: how to convert adjacency list to adjacency matrix 
Python :: connect a mean value to histogram pandas 
Python :: python 7zip extract 
Python :: labs fill ggplot2 
Python :: view all columns pandas 
Python :: python - calculate the value range on a df 
Python :: create 8ball command in discord.py 
Python :: xticks and yticks matplotlib 
Python :: python variable 
Python :: python use functions from another file 
Python :: add cooldown to command discord.py 
Python :: how to check if there is a word in a string in python 
Python :: Python RegEx Findall – re.findall() 
Python :: sklearn predict threshold 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =