Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: read-only attributes

Reply
Thread Tools

Re: read-only attributes

 
 
limodou
Guest
Posts: n/a
 
      02-10-2006
On 2/10/06, john peter <(E-Mail Removed)> wrote:
> while reading the lib manual, i came across mentions of read-only
> attributes.
> for example, a method has a read-only attribute (called _im_self ?) for
> binding
> a class instance to the method. is such a facility available to custom
> application
> code? if so, what do i have to do if i want my application code to have
> read-only
> attributes?
>
>


I think you may consider property() built-in function:

property( [fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive
from object).
fget is a function for getting an attribute value, likewise fset is a
function for setting, and fdel a function for del'ing, an attribute.
Typical use is to define a managed attribute x:


class C(object):
def __init__(self): self.__x = None
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")

New in version 2.2.

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      02-10-2006
limodou wrote:
> On 2/10/06, john peter <(E-Mail Removed)> wrote:

(snip)

>> what do i have to do if i want my application code to have
>>read-only
>> attributes?
>>

> I think you may consider property() built-in function:
>
> property( [fget[, fset[, fdel[, doc]]]])
>
> Return a property attribute for new-style classes (classes that derive
> from object).
> fget is a function


s/function/callable/

> for getting an attribute value, likewise fset is a
> function for setting, and fdel a function for del'ing, an attribute.
> Typical use is to define a managed attribute x:
>
>
> class C(object):
> def __init__(self): self.__x = None
> def getx(self): return self.__x
> def setx(self, value): self.__x = value
> def delx(self): del self.__x
> x = property(getx, setx, delx, "I'm the 'x' property.")


Note that you don't need to define all three accessors. For a
'read-only' attribute, just define the getter:

class ReadOnly(object):
def __init__(self, x):
self._x = x
x = property(fget=lambda self: self._x)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM



Advertisments