0

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?]

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your own initializer, not constructor. The difference is... subtle, and not terribly important until you dig a little deeper.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.