"Protected" property in Python?

Jules Dubois bogus at invalid.tld
Tue Sep 23 01:22:44 EDT 2003


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:

----------------------------------------------------------------
#! /usr/bin/python
class SuperClass(object):
    def __init__(self):
        self.__statusCode = "value bound in SUPERCLASS"
    getStatusCode = property(lambda self: self.__statusCode)

class SubClass(SuperClass):
    def __init__(self):
#        SuperClass.__init__(self)
        self.__statusCode = "value bound in SUBCLASS"

if __name__ == "__main__":
    s = SubClass()
    print s.getStatusCode
----------------------------------------------------------------

If I run this program, I get an exception (output wrapped by hand):

  Traceback (most recent call last):
    File "./xxx.py", line 14, in ?
      print s.getStatusCode
    File "./xxx.py", line 5, in <lambda>
      getStatusCode = property(lambda self: self.__statusCode)
  AttributeError: 'SubClass' object has no attribute \ 
          '_SuperClass__statusCode'

Why is the lambda function attempting to access the superclass' attribute
and not the subclass' attribute?  Can I make it not do that?

If I replace "__statusCode" with "_statusCode", the output is

  value bound in SUBCLASS

as I want.  However, "_statusCode" is then visible from the outside, as I
don't want.

Am I missing something about how Python works?

Is my problem more fundamental, like not understanding OO programming?

I'm able to RTFM if someone would provide a pointer.




More information about the Python-list mailing list