User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2008
Posts: 14
Reputation: ymf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ymf ymf is offline Offline
Newbie Poster

Class getters and setters

  #1  
Jul 2nd, 2008
Hi,
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._lname
What I dont get is what these code blocks do:
def set_fname(self, fname):
        self._fname = fname

 def set_lname(self, lname):
        self._lname = lname
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 139
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 21
jlm699's Avatar
jlm699 jlm699 is offline Offline
Junior Poster

Re: Class getters and setters

  #2  
Jul 9th, 2008
Originally Posted by ymf View Post
What I dont get is what these code blocks do:
def set_fname(self, fname):
        self._fname = fname

 def set_lname(self, lname):
        self._lname = lname
What exactly does the set_fname and set_lname do? and how come the self._fname and self._lname are inaccessible?

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
  1. >>> p1 = Person('James', 'Matthews')
  2. >>> p1
  3. <__main__.Person instance at 0x01C84FD0>
  4. >>> p1.fname()
  5. 'James'
  6. >>> p1._fname
  7. 'James'
  8. >>>
As you can see, I can still access those objects. However the helper function fname() makes it easier on the user.
Let's Go Pens!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Class getters and setters

  #3  
Jul 9th, 2008
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:

  1. def set_angle(self, angle):
  2. 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:

  1. def get_angle(self):
  2. return self._angle
  3.  
  4. def set_angle(self, angle):
  5. self._angle = angle % 360
  6.  
  7. 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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 1:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC