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 :: how to check if variable in python is of what kind 
Python :: import python module 
Python :: Python NumPy concatenate Function Syntax 
Python :: pandas sort by list 
Python :: if queryset is empty django 
Python :: tuplein python 
Python :: docker hub python 
Python :: github downloader 
Python :: pycryptodome rsa encrypt 
Python :: k-means clustering 
Python :: read yml file in python 
Python :: how to read a file in python 
Python :: python create a global variable 
Python :: keras callbacks 
Python :: Showing all column names and indexes dataframe python 
Python :: what is an indefinite loop 
Python :: spreadsheet worksheet counter 
Python :: print treelib.Tree 
Python :: convert from R to python 
Python :: python starting multiple processes in a loop 
Python :: python russian roulette 
Python :: ex:deleate account 
Python :: how write a date with th and nd in python 
Python :: Add dj_database_url on heroku or production 
Python :: PyQt5 change keyboard/tab behaviour in a table 
Python :: is 2 an even number 
Python :: reload python repl 
Python :: select randomly from list in loop 
Python :: python record screen and audio 
Python :: replicate python 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =