Type conversions using as

I think type conversions would be better with using ‘as’ or ‘to’
Rust example

let my_int = 69 as u8;

It makes code more readable

The feature of Python we all like is brief syntax. The most short and actually clean definition of type conversion is using the type name before variable as a unary operator. Using as for that is redefining its functionality.
for example:

n = 5 
sn = str n  # you propose n as str . original is str(n), however you do not need brackets here.
1 Like

Welcome to the forum @Bot-Kerem.

There is only one kind of integer in Python, so it is not necessary to declare a storage size. I find n = 5 readable. I can’t for now think of any kind of expression where you would have a choice of representation.

You can annotate a name with the type you think it ought to be, but it doesn’t make any difference, except to tools that check your code.

>>> n : int = 5
>>> sn : str = n
>>> type(sn)
<class 'int'>

What I mean is that it can be more readable to use ‘as’ instead of type conversion with parentheses, not only when initializing values. I guess the Rust example turned out to be a little bad. This is the Python example:

def calculate_age(year):
    return 2023 - year

year = input("Enter your year of birth: ")
print(f"You are { calculate_age(year as int) } years old")

In other contexts something as name assigns to a variable called name. So your proposed syntax would be inconsistent with that.

from module import name as other_name
with open("filename") as f:
except RuntimeError as e

match x:
  case [1,2,3] as y:

But int(year) isn’t a type conversion, it’s a function call. What is special about the callable int that means it can be called in the form year as int, when other callables like next(iter) can’t be?

It seems that you’re trying to copy over the superficial syntax of Rust, where the underlying semantic model is very different.

8 Likes

You say “more readable”, but what I think you mean is that “as a Rust programmer who is familiar with, and likes, Rust syntax, I would prefer Python copies Rust syntax”.

As a Python programmer, your suggested syntax is much less readable to me, because now I have to try to work out which uses of “as” creates a new name and which make a function call.

var as int  # Calls the function int()
import somemodule as mod  # Import somemodule and assign it to the name "mod"
with var as cm  # Uses var as a context manager and assigns the instance to the name "cm"
except ValueError as e  # Catches a ValueError exception and assigns the instance to the name "e"

Because int() is just a function (actually, a class) and type conversion in Python is always a runtime function call, never a compile-time coercion, allowing var as int would have to allow string as len and x as math.sqrt. Yuck.

And that would mean we could write horrors like this:

try:
    code()
except spam as function as error:
    handle_error()

which is ambiguous. Does that mean…?

except function(spam) as error:

or

except error(function(spam)):

both of which would be legal.

7 Likes