Pushpak Dagade
Pushpak Dagade

Reputation: 6440

Python polymorphism trouble

In python, I defined a class 'number'. Now I wish to define a function named 'number' too. (Just like 'int' or 'str' are classes as well as you can use them as functions ('int()' , 'str()') for type conversion). How can I do the same with (my class and function) 'number'?

[Would it be correct to call this as polymorphism?]

Upvotes: 0

Views: 253

Answers (2)

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

class number(object):
    def __init__(self, value)
        self.val = value # or something like that

n = number(2.3)

int() and str() are not functions, they are type constructors. So you need to make your own constructor initializer.

Upvotes: 3

Rohan Monga
Rohan Monga

Reputation: 1777

what you want are callables. Look at this for more.

Upvotes: 1

Related Questions