0

I'm going from Java to Python and I would like to know if I have the ability to ask a variable to be an integer and never attempt to cast itself in something else.

# A method that return a floating point number
def afloatvalue():
    return 20.1

# Here is a variable i I would like to be an integer
i = 15

# But of course, this assignment changes it to a floating point number.
i = afloatvalue()

print(i)

Output : 20.1 (normal)
But I would like i to be stay absolutely an integer anywhere in the program (like in Java), so that :

i = afloatvalue()

Would lead to 20.

Is it possible by using some keyword when defining it ?

i : int = 15

doesn't grant this guarantee.

Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41
  • tl;dr: not really, but see https://mypy.readthedocs.io/en/stable/ – Konrad Rudolph Jun 14 '21 at 17:27
  • Python is dynamically typed (i.e. variable types are defined in runtime), so no. – enzo Jun 14 '21 at 17:29
  • 1
    Variables themselves have no type; *values* have types, and names can refer to values of any type. A type *hint*, however, can be specified as an annotation to indicate to 3rd-party tools or as documentation that the *intent* is for the name to refer to a value of a specific type. Under no circumstances will a simple assignment perform any kind of type coercion or translation of the value being assigned. – chepner Jun 14 '21 at 17:32
  • In this particular case I consider it a flaw, not a feature that other languages silently truncate floats without any visible syntax indicating it. I wouldn't try to emulate those languages. The fact that Python will require you to explicitly truncate is a good thing. – John Kugelman Jun 14 '21 at 17:33
  • @chepner Doesn't this lead to troubles? How can you enforce them to be integer when it's truly needed and that you're using third party function you don't know about, but are returning numbers ? – Marc Le Bihan Jun 14 '21 at 17:34
  • You are responsible for maintaining the correct type, because there is no single "correct" conversion of a `float` to an `int`: should 20.1 be truncated, rounded to the nearest int, always rounded up, or what? Python can't determine that for you, and so will not try. – chepner Jun 14 '21 at 17:36
  • That said, the built-in `int` and `float` types know about each other, and so things like `3 + 3.5` will work without requiring an explicit conversion. Not because Python does any implicit type coercion, but because `3 + 3.5` is equivalent to `int.__add__(3, 3.5)`, and `int.__add__` knows how to create a `float` value from an `int` and a `float`. – chepner Jun 14 '21 at 17:39
  • Switching from static typing to dynamic typing is a tough cognitive shift. At first you're only going to notice the cons of dynamic typing and it will appear foolish. It will take some time before you can appreciate and internalize the pros. My advice, try to temper your gut reactions to the weird practices you observe. Trust that they are somehow sensible. Have faith. Eventually it will make sense, and you will begin to see downsides of static typing that you never noticed. – John Kugelman Jun 14 '21 at 17:42
  • Aside from the static-dynamic distinction, there is also a strong-weak distinction in typing, with Python on the strong end of the spectrum. That just means values are not implicitly converted to other values: new values need to be constructed, even if the *mechanism* for doing so is hidden in the machinery of the language. (For example, any type of value can be used in an `if` condition, because Python has a well-documented means of obtaining a "truthiness" value from non-Boolean values. Non-zero numbers are true, empty containers are false, etc.) – chepner Jun 14 '21 at 17:46

0 Answers0