LOLPython
(10 ** -2).is_integer()
> False
(10 ** 2).is_integer()
> AttributeError: ‘int’ object has no attribute ‘is_integer’
LOLPython
(10 ** -2).is_integer()
> False
(10 ** 2).is_integer()
> AttributeError: ‘int’ object has no attribute ‘is_integer’
Do you know what ** does?
LikeLike
(now I had to look it up … weird. I don’t understand the first case)
LikeLike
I think it is power
LikeLike
Squared. Used to be. Dunno what it may stand for in Python. Still think it is second power.
LikeLike
It’s exponent and it returns a float in one case and an int the other case. Because expressions can return either type, you’d think it would be wise to have a little polymorphism happening here. Is_integer () is only present on floats.
LikeLike
10 ^ -2 = 0.01
LikeLike
John Hardy Turnbull delenda est I would put the ‘-2’ inside brackets. Just to be sure to be sure.
Eg 10^(-2). The compiler may understand it, but hoomans may stuff up.
LikeLike
To be fair, you started with an integer, and then expected to use a method that’s only in the float class on it.
If you had used 10.0 (a float) instead of 10 (an int) this would have worked. Or even “type(10**2) is int” would probably be better.
I think this is an issue with most scripting languages (or anything that’s auto-typed) where you’re forced to add an empty decimal value to specifically cast it as a float.
That said… fuck python. And type identification functions should be global. Having that “.is_integer()” method specific to floats was bound to lead to confusion.
LikeLike
Dave Maez I really dislike Python, and I believe that it’s never the best solution for any problem, and that the language itself is very poorly designed. So don’t take me as a Python apologist.
But in this case, I think the biggest issue is the name of the method. is_integer is not a type identification method. Instead it simply checks if the number has any decimals.
LikeLike
Satyr Icon: There’s no need to add parentheses because there’s no ambiguity here. ** is always an infix operator in Python, and although it can behave as a quasi-prefix in the syntactic sugar for passing a dict to a procedure as keyword arguments, it can never be misparsed as a postfix operator.
LikeLike
Andres Soolo whilst the Python program may run quite happily, those who do not know Python (raises hand) can see and understand the commas.
Back when I did programming as a subject we were taught Turbo Pascal. And the parenthesis were not necessary then either. But we put them in for humans (well i put them in) who neither knew Turbo Pascal nor its compiler.
I understand Turbo Pascal is now a ‘dead language’, but the modular learning was probably useful.
Self taught in Fortran (66?), C++, Lisp (don’t ask me anything about Lisp) amongst other languages. But forgot probably as much as the last time I wrote any programming was probably mid 90’s.
LikeLike
Satyr Icon: AFAIU, the custom for putting the parens in comes from some algebraic notations. But it has never been universal among people who do algebra on paper.
LikeLike
To be honest I was only having a dig at python because it’s a dynamic language which inevitably has edge cases but which almost never are an issue.
Of course JavaScript has a lot more of these mainly caused by automatic type coercion.
LikeLike