[Tutor] New-style classes

Jan Eden lists at janeden.org
Thu Sep 29 15:00:52 CEST 2005


Hi Kent,

Kent Johnson wrote on 29.09.2005:

>>Data.Base has no base classes, so it is not based on a built-in
>>type: How can it be a new-style class?
>
>Data.Base is not a new-style class, but the classes in Show are. And
>properties may appear to work in old-style classes. This document
>http://www.python.org/2.2.3/descrintro.html#property says,
>Properties do not work for classic classes, but you don't get a
>clear error when you try this. Your get method will be called, so it
>appears to work, but upon attribute assignment, a classic class
>instance will simply set the value in its __dict__ without calling
>the property's set method, and after that, the property's get method
>won't be called either.

Thanks for your quick reply. I read the document you mentioned before, that's why I was confused.

My actual code looks like this:

class Base:
    def GetOwnType(self):
        try: return self._own_type
        except: return self.child_type
    
    def SetOwnType(self, value):
        self._own_type = value
        
    own_type = property(GetOwnType, SetOwnType)

For some of the subclasses of Base, the attribute own_type is defined, the others should use child_type.

For both groups of subclasses, this works fine - if own_type has not been set somewhere else, self.child_type is returned when calling self.own_type.

When checking Data.Base.__mro__, I get an error, so it is not a new-style class by itself.

On the other hand, every time I use the own_type attribute, I do so via instances of new-style classes (Show.Page, Show.Author etc).

Could it be that the nature of these classes makes the code in Data.Base behave according to the new-style rules?

Thanks,

Jan
-- 
I'd never join any club that would have the likes of me as a member. - Groucho Marx


More information about the Tutor mailing list