+ 8
Wierd int() but ok
Hello dear SoloLearners! I found that int() is 0 Isn't it wierd, that integer of nothing is 0? I expected None or error. https://code.sololearn.com/c21raP3KeXb3/?ref=app
10 odpowiedzi
+ 10
just trying to think the int() function should return an int
and the int for nothing is zero.
+ 8
int, rather than being a function, instanciates an object of type int.
int(5.5) for example creates an int-object out of a float which you pass to the constructor of the class int.
If you pass nothing, you'll get an empty int object, so to say, set to zero. You can do that with str (you'll get '') or bool(leads to False) as well.
+ 7
In [1]: int?
Init signature: int(self, /, *args, **kwargs)
Docstring:
int([x]) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments are given.
+ 4
Strings + numbers lol. Most languages just convert it to a string but Python, being Python, throws a tantrum.
+ 3
Thanks Mert Yazıcı ! Good explanation!
+ 3
It is the default. You see. Python already throws loads of tantrums and this one is one it wants to avoid.
+ 2
Ints start at 0, just like arrays.
+ 2
⚡Prometheus ⚡, what sort of tantrums are you thinking of?
+ 2
Ah, so you mean, raising a TypeError in this case?
Aren't most languages throwing errors if you mix up types? The C's, Java...
JS seems to be a bit more lenient; which for me is rather confusing, like if a string doesn't even know that it is a string.
'Ah, there's a plus, so let's concat!'
'Ah, there's any other operator... let me reconsider... am I a number?'
EDIT: Ah, or are you talking about ASCII arithmetics, so to say?
0
Weird flex, but okay...