程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python often looks at function usage and return value type

編輯:Python

Python There are many functions , have access to help() Function to get the usage of the function

help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

At the same time, when we define the function ourselves , You can also explain the function properly

def times(s:str,n:int) ->str: # The return value is str type 
''' return n individual s character string '''
return s*n
print(times(' Beishan ',3))
 Beishan, Beishan, Beishan

meanwhile , have access to .__annotations__ Method to get the type comment of the function

times.__annotations__
{'s': str, 'n': int, 'return': str}

He returns his two parameters in the form of a dictionary , And one. str Return value of type

View function documentation using .__doc__ Method

print(times.__doc__)
 return n individual s character string

In object oriented programming ,python Class has multiple inheritance properties , If the inheritance relationship is too complex , It's hard to see which property or method will be called first .

In order to easily and quickly see the inheritance relationship and order , have access to .__mro__

class X(object):pass
class Y(object):pass
class A(X, Y):pass
class B(Y):pass
class C(A, B):pass
print C.__mro__
# (<class '__main__.C'>, <class '__main__.A'>,
# <class '__main__.X'>, <class '__main__.B'>, 
# <class '__main__.Y'>, <type 'object'>)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved