inherit from data type

Ulrich Eckhardt eckhardt at satorlaser.com
Tue May 11 05:38:42 EDT 2010


Richard Lamboj wrote:
> "How knows python that it is a float, or a string?" Sorry this was bad
> expressed. I want to create a new data type, which inherits from float. I
> just know the "dir" function and the "help" function to get more
> infromations about the class, but i need to get more information about the
> class structure.

I'd suggest the tutorial, see http://docs.python.org, though your question
is already beyond the range of beginners' questions.

> What i also want to know:
>>>> variable1 = 10.50
>>>> type(variable1)
> <type 'float'>
> 
> Is there a way to tell python that it use antoher class than float for
> float, like myfloat? Its just a "tell-me-what-is-possible".
> 
> Sample:
>>>> variable1 = 10.50
>>>> type(variable1)
> <type 'myfloat'>

The expression "10.5" will always be a float, you can't change that. What
you can do is create your own type, derived from float:

  >>> class myfloat(float):
  ...     pass
  ...

and then create an object of that type:

  >>> x = myfloat(3.14)
  >>> type(x)
  <class '__main__.myfloat'>
  >>>                          


Lastly one warning: In many cases where people wanted to derive from a
concrete data type, their problems were easier solved by external code. I
don't know your problem though.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list