Named integers and enums

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Mon May 24 01:29:25 EDT 2004


Thanks for the answers!

Shalabh Chaturvedi wrote:
>Hallvard B Furuseth wrote:
>> Is this possible without too much code?
>> 
>>     class Named_int(object, int):
> 
> (...) Why do you want to subclass object here anyway? int is already a
> subclass of object, so just Named_int(int) should be enough.

I didn't know that.  Can't find it in the python doc either.  But now I
wonder: isinstance() says that even classic class instances are 'object'
instances, even though they do not seem to be (since __slots__ does not
work):

  >>> class o: __slots__ = ()
  ... 
  >>> isinstance(o(), object)
  1
  >>> o().foo = True
  >>> 

It works as expected with subclasses of 'int':

  >>> class n(int): __slots__ = ()
  ... 
  >>> isinstance(n(3), object)
  1
  >>> n(3).foo = True
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  AttributeError: 'n' object has no attribute 'foo'

-- 
Hallvard



More information about the Python-list mailing list