ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Some help with properties and accessor functions....
by Alan Gauld other posts by this author
Apr 19 2006 10:34AM messages near this date
[Tutor] Some help with properties and accessor functions.... | [Tutor] Fw: anybody tried Google calendar?
class SOFileIndexRecord(object):
    def __init__(self, so):
        self._so=so

    def _get_code(self):
        return self._so.code
    def _set_code(self, value):
        self._so.code=value
    testCode=property(_get_code, _set_code) # What does this do?

It says that testCode is a property of SOFileIndexRecord
A property looks like a data attribute from the user of the 
class's point of view but internallty it is accessed via the 
get/set methods.

Thus I can do:

s = SOFileIndexRecord(mySo):
s.testCode = myCode   # actually does s._setCode(myCode)
print s.testCode            # actually s._getCode()

properties allow for creation of read only attributes in Python 
by having a null set method.(Of course you can still cheat and 
use the 'hidden' methods, or even the real data attributes. Buit 
if everyone behaves properly it can help a wee bit.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Mark True
Alan Gauld

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved