Reputation: 6440
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
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