0% found this document useful (0 votes)
10 views7 pages

020 Property Decorators Lecture

The document discusses Python property objects and how they can be instantiated in different ways. Property objects define getter, setter, and deleter methods to access and set attributes. They can be created using the property() function or the @property decorator syntax. Using decorators provides a cleaner way to define properties by automatically assigning getter and setter methods to the property object. This allows properties to behave like regular attributes while under the hood using method calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

020 Property Decorators Lecture

The document discusses Python property objects and how they can be instantiated in different ways. Property objects define getter, setter, and deleter methods to access and set attributes. They can be created using the property() function or the @property decorator syntax. Using decorators provides a cleaner way to define properties by automatically assigning getter and setter methods to the property object. This allows properties to behave like regular attributes while under the hood using method calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

©2019 MathByte Academy

The property Class

The property class can be instantiated in different ways:

x = property(fget=get_x, fset=set_x)

The class defines methods (getter, setter, deleter) that can take a callable as an argument
and returns the instance with the appropriate method now set

Could create it this way instead: x = property()

x = x.getter(get_x)

x = x.setter(set_x)
or

x = property(get_x)
x = x.setter(set_x)

©2019 MathByte Academy


we now have a property language
def MyClass:
with a getter method defined
def __init__(self, language):
self._language = language
remind you of decorators?
def language(self):
return self._language

language = property(language)

Instead we can write:

def MyClass:
def __init__(self, language):
self._language = language

@property
def language(self):
return self._language

Next, we may want to define a setter method as well ©2019 MathByte Academy
def MyClass:
def __init__(self, language):
self._language = language

@property
def language(self):
return self._language
at this point language is now a property instance

def set_language(self, value): this is a setter method


self._language = value
which we need to assign to the
language property

language = language.setter(set_language)

But again, we can rewrite this using the @ decorator syntax:

@language.setter
def language(self, value):
self._language = value
©2019 MathByte Academy
If you find this a bit confusing, think of doing it this way first:

def MyClass:
def __init__(self, language):
self._language = language

@property store a reference to the language object


def language(self): (of type property)
return self._language

redefine the symbol language as a method


lang_prop = language

def language(self, value): assign the setter method to the property object
self._language = value (setter returns the property object itself)

language = lang_prop.setter(language)

make the language symbol refer


to the property object again

del lang_prop delete lang_prop symbol


from namespace ©2019 MathByte Academy
To summarize, we can use decorators to create property type objects as well:

def MyClass:
def __init__(self, language): function name defines the property instance name (symbol)
self._language = language

@property
def language(self):
return self._language language is now a property instance (an object)

we use the setter method of the language property object


@language.setter
def language(self, value)
self._language = value

important to use the same name,


otherwise we end up with a new
symbol for our property!
(we'll see this in the code video)

©2019 MathByte Academy


Coding

©2019 MathByte Academy

You might also like