python使用变量需要初始化吗_在python中“初始化”变量?

我知道你已经接受了另一个答案,但我认为更广泛的问题需要解决-适合当前语言的编程风格。

是的,Python中不需要“初始化”,但是您所做的不是

初始化。它只是对其他语言中的初始化的不完全和错误的模仿。在静态类型语言中初始化的重要一点是指定变量的性质。

在Python中,与在其他语言中一样,在使用变量之前,您确实需要给出它们的值。但是,在函数开始时给它们值并不重要,如果你给的值与它们以后收到的值无关,那就错了。这不是“初始化”,而是“重用”。

我将对您的代码做一些注释和更正:def main():

# doc to define the function

# proper Python indentation

# document significant variables, especially inputs and outputs

# grade_1, grade_2, grade_3, average - id these

# year - id this

# fName, lName, ID, converted_ID

infile = open("studentinfo.txt", "r")

# you didn't 'intialize' this variable

data = infile.read()

# nor this

fName, lName, ID, year = data.split(",")

# this will produce an error if the file does not have the right number of st

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值