def fuction (argument (, argument )* ):
...
Annotation is expressions associated with indicated parts of functions
Stored as a dict
from anntated parameter string to annotation expression
<Function>.__annotaions__ == {'<Parameter>': <AnnotationExpression>, ..., 'return': <AnnotationExpression>}
Annotate argument's type: argument: <Type>
Annotate return type: def fuction(<Arguments>) -> <ReturnType>:
Function with type annotation:
def < FunctionName > (< Argument > : < Type > (, < Argument > : < Type > )* ) -> < ReturnType > :
# ...
Annotation is prefered over type docstr
Pass Reference, not Variable
[Original Variable ] -> [Object ]
[Parameter Variable ] -> [Object ]
Define: <Argument>=<DefaultValue>
Can provide value optionally when invoke
def function (argument = < DefaultValue > ):
...
fuction (argument )
fuction () # Equivalent to fuction(<DefaultValue>)
<DefaultValue>
is a expression, and evaluated when the function defined
2.2. Keyword Parameter 关键字参数
在调用时通常按名称指定值: fuction(argument=value)
在定义函数时, 有以下两种特殊的指示形式参数
/
: 之前的参数只能通过位置参数指定值 Positional Only, 不能通过关键字参数指定值
名称不重要且总是以固定顺序指定值的参数可限定为仅限位置参数指定值 Positional Only
*
: 标志可通过位置参数指定值的参数的结束, 之后的参数只能通过关键字参数指定值 Keyword Only, 不能通过位置参数指定值
名称重要且有助于使函数调用更可读的参数可限定为仅限关键字参数指定值 Keyword Only
Function is Function Variable
Function is Variable which refer to fuction object
E.g.:
def function()
function
is a variable
Refer to a function object
Function Variable is Function
Function Variable can be used as Function: function_variable()
def original_function_variable ():
...
new_function_variable = original_function_variable
new_function_variable ()
input(prompt: str) -> str
Print prompt
without additional ending then read from stdin
and return as str
print(object(, object)*[, sep=sep][, end=''][, file=file][, flush=True])
Print object
to file
, separated by sep
and followed by end
object
: If is str
, print as is; if is not str
, print str(object)
sep
: Default to ' '
end
: Default to '\n'
file
: Default to sys.stdout
flush
: Default to False
all(iterable) -> bool
: 内置函数, 当iterable
中所有元素都为真时返回真, 否则返回假
all(expression_list: iterable) -> bool
all(<ListComprehensionWhitoutBracket>) -> bool
E.g.: all(expression(element) for element in iterable)
Return True
if all element in expression_list
is True
, otherwise False
Short circuiting: Terminate on first False
True
if iterable
is empty
any(iterable) -> bool
: 内置函数, 当iterable
中任意元素为真时返回真, 否则返回假
any(expression_list: iterable) -> bool
any(<ListComprehensionWhitoutBracket>) -> bool
E.g.: any(expression(element) for element in iterable)
Return True
if any element in expression_list
is True
, otherwise False
Short circuiting: Terminate on first True
False
if iterable
is empty
sorted(iterable, *, key=None, reverse=False) -> List
Argument
key
: KEY(ELEMENT: object) -> int
Function extract sort key from each element
Sort with lambda function: lambda <Element>: <SortKey>
E.g.:
sorted(['a', 'ab', 'abc'], key=len)
sorted(['a', 'ab', 'abc'], key=lambda string: len(string))
reverse
: bool
: If sort reversely
Return a NEW (default ascending) sorted list from iterable
Stable
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
Return largest item
Argument
default
: Object to return if provided iterable is empty
key
: key(element: object) -> int
Function extract sort key from each element
Sort with lambda function: lambda <Element>: <SortKey>
callable(object: object) -> boolean
Return if objcet
is callable
hasattr(object: object, name: str) -> bool
Return True
if object
has attr name
getattr(object: object, name: str(, default: object)?) -> object
setattr(object: object, name: str(, value: object)?) -> object
isinstance(object: Object, classinfo: Class) -> bool
Return True
if object
is instance of classinfo
issubclass(class: Class, classinfo: Class) -> bool
Return True
if class
is subclass of classinfo
chr(char_unicode_code_point: int) -> str
Return str
character of given character unicode code point
ord(char_str: str) -> int
Return character unicode code point of given str
character
reversed(Iterable) -> Iterator
返回一个逆序迭代器
abs(number)
: 返回number
的绝对值
open(file, mode='[rwxabt+]', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
: Open file
and return a corresponding file object
mode
r
: Read (default)
w
: Write
x
: Exclusive write, failing if file already exists
a
: Appending
b
: Binary mode
t
: Text mode (default)
+
: Read & Write
encoding
: Usually 'utf-8'
Returned object
Text mode: io.TextIOWrapper
Buffered Binary mode: io.BufferedReader
, io.BufferedWriter
, io.BufferedRandom
Unbuffered Binary mode: io.FileIO
seek(OFFSET, whence=(SEEK_CUR|SEEK_END))
to move cursor
with open (r'<FileDir>' , r'<Mode>' ) as < FileObjectName > :
# ...
vars([object])
: Return __dict__
attribute for object
(default to local scope)