Python (2nd Edition): Learn Python in One Day and Learn It Well. Python for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 1) by Jamie Chan & LCF Publishing
Author:Jamie Chan & LCF Publishing [Chan, Jamie]
Language: eng
Format: mobi
Publisher: Learn Coding Fast
Published: 2017-05-09T16:00:00+00:00
officeStaff1 .
9.4 Properties
Now that we have a basic understanding of classes and objects, let us move on to discuss properties.
In the examples above, we notice that we can access an objectâs instance variables using the dot operator. This makes it easy for us to read the variables and modify them when necessary. However, this flexibility also poses some problems. For instance, we may accidentally change the position of officeStaff1 to a non-existent position. Or we may change the pay of officeStaff1 to an incorrect amount.
To prevent such errors from occurring, we can use properties. Properties provide us with a way to check the values of the changes that we want to make before allowing the change to occur.
To demonstrate how properties work, weâll add one for the variable position . Specifically, weâll add a property to ensure that the variable position can only be set to either 'Basic' or 'Manager' .
However, before we do that, we want to first change the name of the instance variable from position to _position . Adding a single underscore in front of a variable name is a customary way to signal to other programmers that they should not touch this variable directly.
In Python programming, there is a commonly used phrase that says "we're all consenting adults here". We are all expected to behave like an adult. Adding a single underscore in front of a variable tells other programmers that you're trusting them to behave responsibly and not access that variable directly unless they have a compelling reason to. However technically, there is nothing stopping them from accessing the variable. If they so desire, they can still access it by writing
officeStaff1._position
Having said that, letâs just make the following changes to the classdemo.py file to let other âconsenting adultsâ know they should not access position directly:
Change the line
self.position = pPosition
in __init__ to
self._position = pPosition
and the line
return "Position = %s, Name = %s, Pay = %d" %(self.position, self.name, self.pay )
in __str__ to
return "Position = %s, Name = %s, Pay = %d" %(self._position, self.name, self.pay)
Next, letâs look at how to add a property for the _position variable:
Add the following lines to the Staff class in classdemo.py .
@property
def position(self):
print("Getter Method")
return self._position
@position.setter
def position(self, value):
if value == 'Manager' or value == 'Basic':
self._position = value
else:
print('Position is invalid. No changes made.')
Remember to indent the lines above when adding them to the Staff class. If you do not indent them, they do not belong to the Staff class.
The first line above (@property ) is known as a decorator. We wonât go into details about what a decorator is, but simply put, it allows us to alter the functionality of the method that follows. In this case, it changes the first position() method to a property.
This means that it is telling the compiler that whenever users type
officeStaff1.positio n
it should use the position() method that follows to get the value.
This method simply prints the message âGetter Methodâ and returns the value of the variable _position . Due to the @property decorator that changes the method to a property, we do not have to type officeStaff1.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9885)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9770)
The Mikado Method by Ola Ellnestam Daniel Brolund(9763)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8270)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7762)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7750)
Grails in Action by Glen Smith Peter Ledbrook(7678)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7534)
Windows APT Warfare by Sheng-Hao Ma(6567)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6393)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6314)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6188)
Kotlin in Action by Dmitry Jemerov(5033)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4304)
Functional Programming in JavaScript by Mantyla Dan(4028)
Solidity Programming Essentials by Ritesh Modi(3878)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3650)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3600)
The Ultimate iOS Interview Playbook by Avi Tsadok(3571)
