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

2/23/2019 2:06:47 PM

just trying to think

10 Answers

New Answer


just trying to think the int() function should return an int and the int for nothing is zero.


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.


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.


Strings + numbers lol. Most languages just convert it to a string but Python, being Python, throws a tantrum.


Thanks Mert Yazıcı ! Good explanation!


It is the default. You see. Python already throws loads of tantrums and this one is one it wants to avoid.


Ints start at 0, just like arrays.


⚡Prometheus ⚡, what sort of tantrums are you thinking of?


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?


Weird flex, but okay...