Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
python already does raise a very clear and informative
TypeError: unsupported operand type(s) for *: '<type of a>' and '<type of b>'
without the if statementsit's not clever at all, it's reducing run time significantly, there wasn't any notice on exceptions, if there was an indication that said the code had to account for different data types, then good job, but if it doesn't, it's just unnecesary
this isn't pythonic at all. you should rely on duck typing instead of checking for data type. let the
__mul__
method raise an error instead of doing this, since returningNone
when the types aren't what you expect can lead to a lot of unexpected behavior.Please use spoiler flag next time.
This comment is hidden because it contains spoiler information about the solution
Y'all saying it's overthinking the code... It's clever bro keep going!
I don't think this is a good idea, this looks like overengineering the problem.
This will fail for all custom objects that implement a mul magic method, therefore supporting a multiply operation. @Voile has mentioned Fractions and Decimals, but string types can also be multiplied too.
Secondly, if there is a problem with one of the arguments, Python will throw a very nice and informative TypeError exception. You don't need to wrap it in two nested if-statements.
Thirdly, a failed isinstance() comparison will return False which will potentially hide an error and lead to unforeseen things.
I see what you tried to do, but remember that simple is better than complex ;)
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
You definitely don't want to do this. This would fail silently on
Fraction
,Decimal
and other numeric types.or probably @qavaxb is just making sure that only int, float, complex numbers get multiplied and not others, althought the question doesn't states so.
I think this is a lot of unnecessary code
return (result>0)?(true):(false);
There is no need to have paranthesis here, it only adds noise. ?: has lower priority than the less than operator and there is never any reason to have paranthesis around a single constant.
More importantly - you already have a boolean value, no need to convert it again to a boolean value, just return it:
return result > 0;