LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2015, 09:18 AM   #1
Nexusfactor
Member
 
Registered: Jan 2015
Distribution: Ubuntu
Posts: 50

Rep: Reputation: Disabled
Python Getter/Setter Question


I was doing some reading here, and the author is discussing getter/setters in python. He compares the concept with Java.

Near the end he does this:

Code:
class Contact(object):

    def __init__(self, first_name=None, last_name=None, 
                 display_name=None, email=None):
        self.first_name = first_name
        self.last_name = last_name
        self.display_name = display_name
        self.email = email

    def print_info(self):
        print self.display_name, "<" + self.email + ">"            

    def set_email(self, value):
        if '@' not in value:
            raise Exception("This doesn't look like an email address.")
        self._email = value

    def get_email(self):
        return self._email

    email = property(get_email, set_email)
Implements getter/setters, with a exception in the setter property. So far I get it.

Then he proceeds to say, what NOT to do, and demonstrates this:

Code:
class Contact(object):

    def __init__(self, first_name=None, last_name=None, 
                 display_name=None, email=None):
        self.set_first_name(first_name)
        self.set_last_name(last_name)
        self.set_display_name(display_name)
        self.set_email(email)

    def set_first_name(self, value):
        self._first_name = value

    def get_first_name(self):
        return self._first_name

    def set_last_name(self, value):
        self._last_name = value

    def get_last_name(self):
        return self._last_name

    def set_display_name(self, value):
        self._display_name = value

    def get_display_name(self):
        return self._display_name

    def set_email(self, value):
        self._email = value

    def get_email(self):
        return self._email

    def print_info(self):
        print self.display_name, "<" + self.email + ">"
However, besides the exception, isn't that what you did in your previous code above? I'm I missing something? The author implements the email getter/setter methods, and then proceeds to say in the code at the bottom, don't do it that way.

What should be done? How would a python programmer handle creating the contact class with getter/setter properties?
 
Old 06-02-2015, 10:37 AM   #2
Myk267
Member
 
Registered: Apr 2012
Location: California
Posts: 422
Blog Entries: 16

Rep: Reputation: Disabled
Did you read the article? Look under "The Brush and The Canvas" for more details about why you shouldn't use that second style in your post. Even if you don't read the article, the second one is much longer and verbose and offers nothing more than the first.
 
Old 06-02-2015, 11:17 AM   #3
Nexusfactor
Member
 
Registered: Jan 2015
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
I did read the article, but I couldn't fully understand it. Thanks for the explanation anyways Much appreciated.

If I write my classes the way the author specifies is proper practice:

Code:
class Contact(object):

    def __init__(self, first_name=None, last_name=None, 
                 display_name=None, email=None):
        self.first_name = first_name
        self.last_name = last_name
        self.display_name = display_name
        self.email = email

    def print_info(self):
        print self.display_name, "<" + self.email + ">"            

    def set_email(self, value):
        if '@' not in value:
            raise Exception("This doesn't look like an email address.")
        self._email = value

    def get_email(self):
        return self._email

    email = property(get_email, set_email)


First, how then would the exception be raised if I create my object using the constructor? He no longer calls the set property from the constructor, which he says NOT to do.

Secondly, Why didn't he use property decorators? Wouldn't that be a better option?



Forgive me, I new to Python, and I'm just trying to wrap my head around certain concepts.

Last edited by Nexusfactor; 06-02-2015 at 11:25 AM.
 
Old 06-02-2015, 11:37 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The only explanation I could imagine is that the property decorator might not have been available back then. You are correct that today, it would be written like this:

Code:
class Contact(object):

    def __init__(self, first_name=None, last_name=None, 
                 display_name=None, email=None):
        self.first_name = first_name
        self.last_name = last_name
        self.display_name = display_name

         # This calls the setter property.
        self.email = email

    def print_info(self):
        print self.display_name, "<" + self.email + ">"            

    @property
    def email(self):
        return self._email

    @email.setter
    def email(self, value):
        if '@' not in value:
            raise Exception("This doesn't look like an email address.")
        self._email = value

Last edited by dugan; 06-02-2015 at 11:42 AM.
 
1 members found this post helpful.
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Net setter ARUNKPUSHPAKARAN Ubuntu 1 08-17-2012 06:30 AM
LXer: Why Linux Has Been an Attention Getter Lately LXer Syndicated Linux News 0 08-14-2012 08:50 AM
java : object oriented , how it works and how to set the getter and setter ? whiteshark2012 Programming 1 02-28-2011 11:01 AM
Fluxbox 0.9.13 fbsetbg not detecting any bg setter debiant Linux - Desktop 1 08-18-2006 03:12 PM
Good wallpaper setter (image viewer) Creak Linux - Software 2 06-02-2004 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration