Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Implementing Java-style getters and setters in Python

"""
When a member needs to be slightly protected and cannot be simply 
exposed as a public member, use Python’s property decorator to 
accomplish the functionality of getters and setters.
"""
class Square(object):
    def __init__(self, length):
        self._length = length

    @property
    def length(self):
        return self._length

    @length.setter
    def length(self, value):
        self._length = value

    @length.deleter
    def length(self):
        del self._length

r = Square(5)
r.length  # automatically calls getter
r.length = 6  # automatically calls setter
Comment

PREVIOUS NEXT
Code Example
Python :: Strings Formatting Old Way 
Python :: How to print specific figure in python 
Python :: data structures in python 
Python :: generate natural numbers python 
Python :: calculate values in a certain percentile pandas 
Python :: how to reassign a key py 
Python :: python os module using stat 
Python :: python using boolean len comparing with 
Python :: Abstract Model inherit from another model django 
Python :: python get object attributes 
Python :: upper method in python 
Python :: spark sparsevector to list 
Python :: arabic text recognition from pdf using python 
Python :: machine learning project outline 
Python :: how to loop through glob.iglob iterator 
Python :: File "demo_indentation_test.py", line 2 print("Five is greater than two!") ^ IndentationError: expected an indented block 
Python :: nltk document 
Python :: wxPython wx.Window Connect example 
Python :: print greeting in python explication 
Python :: inspect rows in dictionary pthon 
Python :: promedio en pandas 
Python :: python pass statement 
Python :: install plotly manually 
Python :: Ignoring NaNs with str.contains 
Python :: odoo 8 request.session.authenticate 
Python :: python list comprehension exercises 
Python :: ridge regression alpha values with cross validation score plot 
Python :: the most effective search algorithm in python 
Python :: sample k-means clustering 
Python :: python package for facial emotion recognition 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =