"Protected" property in Python?

Camilo Olarte colarte at telesat.com.co
Tue Sep 23 04:09:49 EDT 2003


On Mon, 22 Sep 2003 23:22:44 -0600
Jules Dubois <bogus at invalid.tld> wrote:

> I'm want to create a superclass with nothing but attributes and properties.
> Some of the subclasses will do nothing but provide values for the
> attributes.  
> (I'd also like to make sure (1) that the subclass provides actual values
> for the attributes and (2) that no "client" module adds or removes
> attributes or properties, but I don't know how to do those.)
> I don't understand what I'm doing wrong, or maybe what I want to do is
> impossible.  Here's a stripped down version of the code:

	Question : why didn't you initialize your super class????
if you do it passing the values you want as arguments then you could have a code 
like : 
<PYTHON>
#! /usr/bin/python
class SuperClass(object):
    def __init__(self,stat_code):
#        self.__statusCode = "value bound in SUPERCLASS"
        self.__statusCode = stat_code
    getStatusCode = property(lambda self: self.__statusCode)
class SubClass(SuperClass):
    def __init__(self):
        self.__statusCode = "value bound in SUBCLASS"
        SuperClass.__init__(self,self.__statusCode  )
if __name__ == "__main__":
    s = SubClass()
    print s.getStatusCode 
    print
    try :
        print s.__statusCode
    except :
        print "cannot do that.. print s.__statusCode"
</PYTHON>

where you don't have private attributes visible and yet you have the values you
passed from the subclass.. 

NOTE : later on accessing those values from your sublass other than from the SuperClass initialization is other story...(setattribute,getattribute...or self.functions)
In your local python documentation.. read the tutorial section on classes:
/python2.2-doc/html/tut/node11.htm





More information about the Python-list mailing list