返回信息流以前一直做c/c+, 感觉不是很适应 特别是参数连个类型都没有明确表示
来自「北邮人论坛手机版」
这是一条镜像帖。来源:北邮人论坛 / python / #7353同步于 2015/6/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
有大神唠唠用python的感受吗
tycoon0
2015/6/11镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
小程序很舒服,因为你显然知道变量是什么类型。
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(10))
但是超过100行代码,也许Python就不是最好的语言了。类型变得很重要,你需要在文档里注明接口,或者添加注释。
def query_students_by_department_age(department, age):
"""
Find students in the database by department and age.
`department` is a `str`.
`age` is a tuple `(pred, num)` where `pred` is one of `"LT"` or `"LE"` , and `num` is an `int`.
Returns a list of tuples. Each tuple is a `(name, age)` pair where `name` is a `str` and `age` is an `int`.
"""
return database.make_query(........)
或者用Python3的类型标注。
def query_students_by_department_age(department: str, age: (OneOf("LT", "LE"), int)) -> [(str, int)]:
"""
Find students in the database by department and age.
`department` is a `str`.
`age` is a tuple `(pred, num)` where `pred` is one of `"LT"` or `"LE"` , and `num` is an `int`.
Returns a list of tuples. Each tuple is a `(name, age)` pair where `name` is a `str` and `age` is an `int`.
"""
return database.make_query(........)
当然Python不会帮你检查类型,也没有官方统一的类型标注方法。