View Full Version : علامت -> بعد از تعریف فانکشن چیست؟
roohial
یک شنبه 09 اسفند 1394, 17:23 عصر
دوستان کسی میدونه که علامت -> بعد از تعریف فانکشن چیست؟
مثل تابع زیر
def f1(a,b) -> int
return a+b
در کد بالا هدف یعنی چه؟ برا چی میاد؟ اگه نیاد چی میشه؟
IamOverlord
جمعه 21 اسفند 1394, 12:08 عصر
سلام!
تا جایی که من اطلاع دارم در پایتون ۲ همچین قابلیت هایی وجود نداره..
این یکی از قابلیت هاییه که شما به عنوان حاشیه نویسی تابع (function annotation) می تونید در پایتون ۳ استفاده کنید.. مثلا در این حالت برای اینه که مشخص کنید خروجی تابع قراره int باشه نه چیز دیگه ای.. و اختیارین.. و به نظر می رسه پایتون به خودی خود اهمیتی بهشون نمی ده.. یه سری برنامه های دیگه ولی می تونن از این توضیحاتی که شما به این صورت می ذارید استفاده کنن و شما رو راهنمایی کنن یا هشدارهایی بهتون بدن..
مثلا این جا رو ببینید:
The only way that annotations take on meaning is when they are interpreted by third-party libraries. These annotation consumers can do anything they want with a function's annotations. For example, one library might use string-based annotations to provide improved help messages, like so:
def compile(source: "something compilable",
filename: "where the compilable thing comes from",
mode: "is this a single statement or a suite?"):
...
Another library might be used to provide typechecking for Python functions and methods. This library could use annotations to indicate the function's expected input and return types, possibly something like:
def haul(item: Haulable, *vargs: PackAnimal) -> Distance:
...
However, neither the strings in the first example nor the type information in the second example have any meaning on their own; meaning comes from third-party libraries alone.
(از https://www.python.org/dev/peps/pep-3107 (https://www.python.org/dev/peps/pep-3107/))
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.