2.2 properties and subclasses

Anton Muhin antonmuhin at sendmail.ru
Wed May 21 14:07:06 EDT 2003


Miles Egan wrote:
> This prints "BASE" instead of "SUB".  Is this right?
> 
> class BaseClass(object):
>     def __init__(self):
>         self._prop = "BASE"
> 
>     def get_prop(self): return self._prop
>     prop = property(get_prop)
> 
> class SubClass(BaseClass):
>     def get_prop(self): return "SUB"
> 
> b = BaseClass()
> print b.prop
> 
> s = SubClass()
> print s.prop
> 
> 
  Just my 2 cents:

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) - 
see 'Help/About PythonWin' for further copyright information.
 >>> class BaseClass(object):
... 	def __init__(self):
... 		self._prop = "BASE"
... 	def get_property(self): return self._prop
... 	def __get_property__(self): return self.get_property()
... 	prop = property(__get_property__)
... 	
 >>> class SubClass(BaseClass):
... 	def get_property(self): return "SUB"
... 	
 >>> b = BaseClass()
 >>> print b.prop
BASE
 >>> s = SubClass()
 >>> print s.prop
SUB
 >>>

hth,
anton.





More information about the Python-list mailing list