There is this code:
#include <iostream>
class SomeClass
{
int someInteger;
float someFloat;
public:
SomeClass(int someInteger_)
{
// do something for int
std::cout << "Int constructor\n";
}
SomeClass(float someFloat_)
{
// do something for float
std::cout << "Float constructor\n";
}
};
int main()
{
SomeClass a(2);
SomeClass b(2.0f);
return 0;
}
Objects of class SomeClass are differently created when float or int arguments for constructors are passed. There is similar class in Python:
class SomeClass:
someInteger = 0
someFloat = 0.0
def __init__(self, value):
# I want to do different things when int or float is passed
print value
a = SomeClass(2)
b = SomeClass(2.0)
General question is – how to make function behavior dependent from types of passed arguments?
Use
isinstance:Or for you constructor: