+ 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

23rd Feb 2019, 2:06 PM
just trying to think
just trying to think - avatar
10 Réponses
+ 10
just trying to think the int() function should return an int and the int for nothing is zero.
23rd Feb 2019, 2:12 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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.
23rd Feb 2019, 3:12 PM
HonFu
HonFu - avatar
+ 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.
23rd Feb 2019, 2:18 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
Strings + numbers lol. Most languages just convert it to a string but Python, being Python, throws a tantrum.
24th Feb 2019, 4:01 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
Thanks Mert Yazıcı ! Good explanation!
23rd Feb 2019, 2:27 PM
just trying to think
just trying to think - avatar
+ 3
It is the default. You see. Python already throws loads of tantrums and this one is one it wants to avoid.
24th Feb 2019, 3:59 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Ints start at 0, just like arrays.
23rd Feb 2019, 10:39 PM
Sandakan Space MAX
Sandakan Space MAX - avatar
+ 2
⚡Prometheus ⚡, what sort of tantrums are you thinking of?
24th Feb 2019, 4:00 PM
HonFu
HonFu - avatar
+ 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?
24th Feb 2019, 4:07 PM
HonFu
HonFu - avatar
0
Weird flex, but okay...
25th Feb 2019, 10:55 PM
Monique Taylar
Monique Taylar - avatar