如何在python中声明变量类型,C风格 [英] how to declare variable type, C style in python

查看:34
本文介绍了如何在python中声明变量类型,C风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名编程专业的学生,​​我的老师从 C 开始教我们编程范式,他说如果我用 python 交作业就可以了(作业更容易更快).我希望我的代码尽可能接近纯 C.
问题是:
我如何像在 C 中一样在 python 中声明变量的数据类型.例如:

I'm a programming student and my teacher is starting with C to teach us the programming paradigms, he said it's ok if I deliver my homework in python (it's easier and faster for the homeworks). And I would like to have my code to be as close as possible as in plain C.
Question is:
How do I declare data types for variables in python like you do in C. ex:

int X,Y,Z;

我知道我可以在 python 中做到这一点:

I know I can do this in python:

x = 0
y = 0
z = 0

但这似乎需要做很多工作,而且它忽略了 Python 比 C 更容易/更快这一点.那么,最短的方法是什么?
附言我知道大多数时候你不必必须在 python 中声明数据类型,但我仍然想这样做,这样我的代码看起来尽可能像同学的.

But that seems a lot of work and it misses the point of python being easier/faster than C. So, whats the shortest way to do this?
P.S. I know you don't have to declare the data type in python most of the time, but still I would like to do it so my code looks as much possible like classmates'.

推荐答案

Python 3.5 引入了类型提示,它引入了一种指定方式变量的类型.此答案是在此功能可用之前编写的.

Python 3.5 introduced type hints which introduced a way to specify the type of a variable. This answer was written before this feature became available.

在 Python 中没有办法声明变量,因为声明"都没有.也不是变量"在C意义上存在.这会将三个名称绑定到同一个对象:

There is no way to declare variables in Python, since neither "declaration" nor "variables" in the C sense exist. This will bind the three names to the same object:

x = y = z = 0

这篇关于如何在python中声明变量类型,C风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆