•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 403,516 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,867 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 304 | Replies: 2
![]() |
•
•
Join Date: May 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
Hi,
I have a code that creates a class 'Person' :
What I dont get is what these code blocks do:
What exactly does the set_fname and set_lname do? and how come the self._fname and self._lname are inaccessible?
Thanks in advance,
ymf
I have a code that creates a class 'Person' :
class Person:
def __init__(self, fname, lname):
self._fname = fname
self._lname = lname
def fname(self):
return self._fname
def set_fname(self, fname):
self._fname = fname
def lname(self):
return self._lname
def set_lname(self, lname):
self._lname = lname
def name(self):
return self._fname + ' ' + self._lnamedef set_fname(self, fname):
self._fname = fname
def set_lname(self, lname):
self._lname = lnameThanks in advance,
ymf
•
•
•
•
What I dont get is what these code blocks do:
What exactly does the set_fname and set_lname do? and how come the self._fname and self._lname are inaccessible?def set_fname(self, fname): self._fname = fname def set_lname(self, lname): self._lname = lname
Those blocks of code reassign the value of _lname and _fname. self._fname and self._lname are not technically inaccessible, they are just simply hidden to the common user. If you typ
python Syntax (Toggle Plain Text)
>>> p1 = Person('James', 'Matthews') >>> p1 <__main__.Person instance at 0x01C84FD0> >>> p1.fname() 'James' >>> p1._fname 'James' >>>
Let's Go Pens!
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Just to jlm's good points above, the getter and setter functions allow the object to control access to its own data members.
For instance, suppose you have a sprite that can rotate. Clearly, if the rotation is greater than 360 degrees, then you want to reduce the rotation to its reference angle between 0 and 360. So you would write:
Now, set_angle controls access to self._angle so that it always has a valid value.
Hence, the data member self._angle is private and is only ever accessed by the object itself. That guarantees that some outside client can't dork up _angle by setting it to an illegal value.
But the method to set it, set_angle, is public.
This is VERY important concept in object-oriented programming: data members should be private; methods should be public (there are exceptions, but that's the good rule-of-thumb).
One more thing: having defined your getter and setter functions, you can now create a public data member that looks like a regular data member, but restricts access as described above. Here's how:
Now, you can make statements like this:
self.angle = 450
and the setter function will be automagically called just as if you had written
self.set_angle(450)
So the real prize in writing getter and setter functions is that you can protect your object from illegal values; from there, public properties allow you to set values without the awkward method calls.
Hope that helps,
Jeff
For instance, suppose you have a sprite that can rotate. Clearly, if the rotation is greater than 360 degrees, then you want to reduce the rotation to its reference angle between 0 and 360. So you would write:
Python Syntax (Toggle Plain Text)
def set_angle(self, angle): self._angle = angle % 360
Now, set_angle controls access to self._angle so that it always has a valid value.
Hence, the data member self._angle is private and is only ever accessed by the object itself. That guarantees that some outside client can't dork up _angle by setting it to an illegal value.
But the method to set it, set_angle, is public.
This is VERY important concept in object-oriented programming: data members should be private; methods should be public (there are exceptions, but that's the good rule-of-thumb).
One more thing: having defined your getter and setter functions, you can now create a public data member that looks like a regular data member, but restricts access as described above. Here's how:
Python Syntax (Toggle Plain Text)
def get_angle(self): return self._angle def set_angle(self, angle): self._angle = angle % 360 angle = property(get_angle, set_angle)
Now, you can make statements like this:
self.angle = 450
and the setter function will be automagically called just as if you had written
self.set_angle(450)
So the real prize in writing getter and setter functions is that you can protect your object from illegal values; from there, public properties allow you to set values without the awkward method calls.
Hope that helps,
Jeff
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Accessing boolean Values of another values in one class. (Java)
- getters&setters (Java)
- Need help with one line of code :( (C++)
- Java tutor needed BADLY (Java)
- Basketball timer help needed (Java)
- Need Help on some methods (Java)
- Java class design question. (Java)
- object oriented programme help (Java)
- lost on writing a Java class (Java)
Other Threads in the Python Forum
- Previous Thread: How to send broadcast message over network and collect all the IPaddress?
- Next Thread: subscript and superscript help!


Linear Mode