(11) chr(i)
Return the string representing a character whose Unicode code point is the integer?i. For example,?chr(97)?returns the string?'a', while?chr(8364)?returns the string?'€'. This is the inverse of?ord().
The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16).?ValueErrorwill be raised if?i?is outside that range.
返回一個(gè)將一個(gè)unicode編碼的整數(shù)表示成字符串课蔬。例如...。chr()是ord()的逆過程。其中參數(shù)i的范圍大小是1114111(0x10FFFF 16進(jìn)制)乎芳,如果i超過這個(gè)值就會(huì)報(bào)錯(cuò)肩袍。
(12) classmethod
Transform a method into a class method.
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:
The?@classmethod?form is a function?decorator?– see the description of function definitions in?Function definitions?for details.
It can be called either on the class (such as?C.f()) or on an instance (such as?C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.
Class methods are different than C++ or Java static methods. If you want those, see?staticmethod()?in this section.
For more information on class methods, consult the documentation on the standard type hierarchy in?The standard type hierarchy.
將一個(gè)方法變成一個(gè)類方法
一個(gè)類方法的第一個(gè)參數(shù)需要是表示自身類的 cls 參數(shù)朦乏,就像實(shí)例方法一樣誊涯。
@classmethod是一個(gè)裝飾器方法迹蛤,如果想了解更多裝飾器方法定義的細(xì)節(jié)請(qǐng)出門左轉(zhuǎn)...(右鍵奇颠,新鏈接打開)
(13)?compile(source,?filename,?mode,?flags=0,?dont_inherit=False,?optimize=-1)
Compile the?source?into a code or AST object. Code objects can be executed by?exec()?or?eval().source?can either be a normal string, a byte string, or an AST object. Refer to the?ast?module documentation for information on how to work with AST objects.
The?filename?argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('<string>'?is commonly used).
The?mode?argument specifies what kind of code must be compiled; it can be?'exec'?if?sourceconsists of a sequence of statements,?'eval'?if it consists of a single expression, or?'single'?if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than?None?will be printed).
The optional arguments?flags?and?dont_inherit?control which?future statements?affect the compilation of?source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling?compile(). If the?flags?argument is given and?dont_inherit?is not (or is zero) then the future statements specified by the?flagsargument are used in addition to those that would be used anyway. If?dont_inherit?is a non-zero integer then the?flags?argument is it – the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the?compiler_flagattribute on the?_Feature?instance in the?__future__?module.
The argument?optimize?specifies the optimization level of the compiler; the default value of?-1selects the optimization level of the interpreter as given by?-O?options. Explicit levels are?0?(no optimization;?__debug__?is true),?1?(asserts are removed,?__debug__?is false) or?2?(docstrings are removed too).
This function raises?SyntaxError?if the compiled source is invalid, and?ValueError?if the source contains null bytes.
If you want to parse Python code into its AST representation, see?ast.parse().
Note
When compiling a string with multi-line code in?'single'?or?'eval'?mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete statements in the?code?module.
Warning
It is possible to crash the Python interpreter with a sufficiently large/complex string when compiling to an AST object due to stack depth limitations in Python’s AST compiler.
Changed in version 3.2:?Allowed use of Windows and Mac newlines. Also input in?'exec'?mode does not have to end in a newline anymore. Added the?optimize?parameter.
Changed in version 3.5:?Previously,?TypeError?was raised when null bytes were encountered in?source.
將源代碼編譯為代碼或AST對(duì)象败去。代碼對(duì)象可以由exec()或eval()執(zhí)行。source可以是普通字符串烈拒,byte字符串或AST對(duì)象圆裕。有關(guān)如何使用AST對(duì)象的信息,請(qǐng)參閱AST模塊文檔荆几。
filename這個(gè)參數(shù)需要傳遞一些可以被代碼所識(shí)別的字符串
mode參數(shù)必須編指定譯哪種代碼;如果source包含一系列語(yǔ)句吓妆,則可以是'exec',如果它由單個(gè)表達(dá)式組成吨铸,則為'eval';如果由單個(gè)交互式語(yǔ)句組成行拢,則為'single'(在后一種情況下,表達(dá)式語(yǔ)句評(píng)估為某個(gè)除了無(wú)以外將被打拥ā)指定編譯哪種代碼;如果source包含一系列語(yǔ)句舟奠,則可以是'exec',如果它由單個(gè)表達(dá)式組成房维,則為'eval';如果由單個(gè)交互式語(yǔ)句組成沼瘫,則為'single'(在后一種情況下,表達(dá)式語(yǔ)句評(píng)估為某個(gè)除了無(wú)以外將被打恿)
(14)divmod(a,b)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as?(a?//?b,?a?%?b). For floating point numbers the result is?(q,?a?%?b), where?q?is usually?math.floor(a?/?b)?but may be 1 less than that. In any case?q?*?b?+?a?%?b?is very close to?a, if?a?%?bis non-zero it has the same sign as?b, and?0?<=?abs(a?%?b)?<?abs(b).
傳入兩個(gè)非復(fù)(復(fù):復(fù)數(shù))的數(shù)字耿戚,返回一個(gè)商和一個(gè)余數(shù),結(jié)果類似于(a?//?b,?a?%?b),對(duì)于浮點(diǎn)數(shù)來說結(jié)是?(q,?a?%?b)?膜蛔,q?通常是?math.floor(a?/?b)?但可能會(huì)比 1 小坛猪。在任何情況下,?q?*?b?+?a?%?b?和?a?基本相等皂股;如果?a?%?b?非零墅茉,它的符號(hào)和?b?一樣,并且?0?<=?abs(a?%?b)?<?abs(b) (后半部分是搬過來的,總之就是除法的功能)
(15)dir()
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
If the object has a method named?__dir__(), this method will be called and must return the list of attributes. This allows objects that implement a custom__getattr__()?or?__getattribute__()?function to customize the way?dir()reports their attributes.
If the object does not provide?__dir__(), the function tries its best to gather information from the object’s?__dict__?attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom?__getattr__().
The default?dir()?mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
If the object is a module object, the list contains the names of the module’s attributes.
If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
太長(zhǎng)了屑墨,dir實(shí)際上就是__dir__,如果不傳參數(shù)躁锁,就最大限度的去尋找本地作用域內(nèi)包含的方法,如果傳入一個(gè)帶有__dir__方法的對(duì)象卵史,這個(gè)方法就會(huì)返回__dir__內(nèi)定義的屬性战转,如果沒有該方法,就會(huì)自己去尋找以躯。下面是中文文檔的翻譯:
如果沒有實(shí)參槐秧,則返回當(dāng)前本地作用域中的名稱列表。如果有實(shí)參忧设,它會(huì)嘗試返回該對(duì)象的有效屬性列表刁标。
如果對(duì)象有一個(gè)名為?__dir__()?的方法,那么該方法將被調(diào)用址晕,并且必須返回一個(gè)屬性列表膀懈。這允許實(shí)現(xiàn)自定義?__getattr__()?或?__getattribute__()?函數(shù)的對(duì)象能夠自定義?dir()?來報(bào)告它們的屬性。
如果對(duì)象不提供?__dir__()谨垃,這個(gè)函數(shù)會(huì)嘗試從對(duì)象已定義的?__dict__?屬性和類型對(duì)象收集信息启搂。結(jié)果列表并不總是完整的,如果對(duì)象有自定義?__getattr__()刘陶,那結(jié)果可能不準(zhǔn)確胳赌。
默認(rèn)的?dir()?機(jī)制對(duì)不同類型的對(duì)象行為不同,它會(huì)試圖返回最相關(guān)而不是最全的信息:
如果對(duì)象是模塊對(duì)象匙隔,則列表包含模塊的屬性名稱疑苫。
如果對(duì)象是類型或類對(duì)象,則列表包含它們的屬性名稱纷责,并且遞歸查找所有基類的屬性捍掺。
否則,列表包含對(duì)象的屬性名稱再膳,它的類屬性名稱乡小,并且遞歸查找它的類的所有基類的屬性。
(16)enumerate(iterable,?start=0)
Return an enumerate object.?iterable?must be a sequence, an?iterator, or some other object which supports iteration. The?__next__()?method of the iterator returned by?enumerate()?returns a tuple containing a count (from?start?which defaults to 0) and the values obtained from iterating over?iterable.
枚舉類型饵史,輸入一個(gè)可迭代對(duì)象,返回一個(gè)可迭代對(duì)象,可選參數(shù)start默認(rèn)為0胳喷,即從第0位開始輸出湃番。
(17)eval(xpression,?globals=None,?locals=None)
The arguments are a string and optional globals and locals. If provided,?globalsmust be a dictionary. If provided,?locals?can be any mapping object.
The?expression?argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the?globals?and?locals?dictionaries as global and local namespace. If the?globals?dictionary is present and does not contain a value for the key?__builtins__, a reference to the dictionary of the built-in module?builtins?is inserted under that key before?expression?is parsed. This means that?expression?normally has full access to the standard?builtins?module and restricted environments are propagated. If the?localsdictionary is omitted it defaults to the?globals?dictionary. If both dictionaries are omitted, the expression is executed in the environment where?eval()?is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions.?
輸入的第一個(gè)參數(shù)是一個(gè)string類型的python表達(dá)式,相當(dāng)于將一個(gè)字符串內(nèi)數(shù)據(jù)解析成python腳本吭露,所以傳入的參數(shù)必須符合python語(yǔ)法規(guī)則才能執(zhí)行吠撮,global必須是一個(gè)字典,locals可以是任何映射對(duì)象(例如zip())讲竿。
(18)exec(object[,?globals[,?locals]])
This function supports dynamic execution of Python code.?object?must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).?[1]?If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the?return?and?yield?statements may not be used outside of function definitions even within the context of code passed to theexec()?function. The return value is?None.
In all cases, if the optional parts are omitted, the code is executed in the current scope. If only?globals?is provided, it must be a dictionary, which will be used for both the global and the local variables. If?globals?and?locals?are given, they are used for the global and local variables, respectively. If provided,?localscan be any mapping object. Remember that at module level, globals and locals are the same dictionary. If exec gets two separate objects as?globals?and?locals, the code will be executed as if it were embedded in a class definition.
If the?globals?dictionary does not contain a value for the key?__builtins__, a reference to the dictionary of the built-in module?builtins?is inserted under that key. That way you can control what builtins are available to the executed code by inserting your own?__builtins__?dictionary into?globals?before passing it to?exec().
(解釋來源:中文文檔)
這個(gè)函數(shù)支持動(dòng)態(tài)執(zhí)行 Python 代碼泥兰。object?必須是字符串或者代碼對(duì)象。如果是字符串题禀,那么該字符串將被解析為一系列 Python 語(yǔ)句并執(zhí)行(除非發(fā)生語(yǔ)法錯(cuò)誤)鞋诗。[1]?如果是代碼對(duì)象,它將被直接執(zhí)行迈嘹。在任何情況下削彬,被執(zhí)行的代碼都需要和文件輸入一樣是有效的(見參考手冊(cè)中關(guān)于文件輸入的章節(jié))。請(qǐng)注意即使在傳遞給?exec()?函數(shù)的代碼的上下文中秀仲,return?和?yield?語(yǔ)句也不能在函數(shù)定義之外使用融痛。該函數(shù)返回值是?None?。
無(wú)論哪種情況神僵,如果省略了可選參數(shù)雁刷,代碼將在當(dāng)前范圍內(nèi)執(zhí)行。如果提供了?globals參數(shù)保礼,就必須是字典類型沛励,而且會(huì)被用作全局和本地變量。如果同時(shí)提供了?globals?和?locals?參數(shù)氓英,它們分別被用作全局和本地變量侯勉。如果提供了?locals?參數(shù),則它可以是任何映射型的對(duì)象铝阐。請(qǐng)記住在模塊層級(jí)址貌,全局和本地變量是相同的字典。如果 exec 有兩個(gè)不同的?globals?和?locals?對(duì)象徘键,代碼就像嵌入在類定義中一樣執(zhí)行练对。
如果?globals?字典不包含?__builtins__?鍵值,則將為該鍵插入對(duì)內(nèi)建?builtins?模塊字典的引用吹害。因此螟凭,在將執(zhí)行的代碼傳遞給?exec()?之前,可以通過將自己的?__builtins__?字典插入到?globals?中來控制可以使用哪些內(nèi)置代碼它呀。
(19)filter(function, iterable)
Construct an iterator from those elements of?iterable?for which?function?returns true.?iterable?may be either a sequence, a container which supports iteration, or an iterator. If?function?is?None, the identity function is assumed, that is, all elements of?iterable?that are false are removed.
Note that?filter(function,?iterable)?is equivalent to the generator expression?(item?for?item?in?iterable?if?function(item))?if function is not?None?and?(item?for?item?in?iterable?if?item)?if function is?None.
See?itertools.filterfalse()?for the complementary function that returns elements of?iterable?for which?function?returns false.
用?iterable?中函數(shù)?function?返回真的那些元素螺男,構(gòu)建一個(gè)新的迭代器棒厘。iterable?可以是一個(gè)序列,一個(gè)支持迭代的容器下隧,或一個(gè)迭代器奢人。如果?function?是?None?,則會(huì)假設(shè)它是一個(gè)身份函數(shù)淆院,即?iterable?中所有返回假的元素會(huì)被移除何乎。
請(qǐng)注意,?filter(function,?iterable)?相當(dāng)于一個(gè)生成器表達(dá)式土辩,當(dāng) function 不是?None?的時(shí)候?yàn)?(item?for?item?in?iterable?if?function(item))支救;function 是?None?的時(shí)候?yàn)?(item?for?item?in?iterable?if?item)?。
請(qǐng)參閱?itertools.filterfalse()?了解拷淘,只有?function?返回 false 時(shí)才選取?iterable?中元素的補(bǔ)充函數(shù)各墨。
這個(gè)方法比較常用,用于過濾掉一些數(shù)據(jù)集內(nèi)不符合條件的數(shù)據(jù)。
(20)float([x])
Return a floating point number constructed from a number or string?x.
If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be?'+'?or?'-'; a?'+'?sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or a positive or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed:
返回從數(shù)字或字符串?x?生成的浮點(diǎn)數(shù)辕棚。
如果實(shí)參是字符串欲主,則它必須是包含十進(jìn)制數(shù)字的字符串,字符串前面可以有符號(hào)逝嚎,之前也可以有空格扁瓢。可選的符號(hào)有?'+'?和?'-'?补君;?'+'?對(duì)創(chuàng)建的值沒有影響引几。實(shí)參也可以是 NaN(非數(shù)字)、正負(fù)無(wú)窮大的字符串挽铁。確切地說伟桅,除去首尾的空格后,輸入必須遵循以下語(yǔ)法:
Here?floatnumber?is the form of a Python floating-point literal, described in?Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY” and “iNfINity” are all acceptable spellings for positive infinity.
Otherwise, if the argument is an integer or a floating point number, a floating point number with the same value (within Python’s floating point precision) is returned. If the argument is outside the range of a Python float, an?OverflowError?will be raised.
For a general Python object?x,?float(x)?delegates to?x.__float__().
If no argument is given,?0.0?is returned.
這里叽掘,?floatnumber?是 Python 浮點(diǎn)數(shù)的字符串形式楣铁,詳見?浮點(diǎn)數(shù)字面值。字母大小寫都可以更扁,例如盖腕,“inf”、“Inf”浓镜、“INFINITY”溃列、“iNfINity” 都可以表示正無(wú)窮大。
另一方面膛薛,如果實(shí)參是整數(shù)或浮點(diǎn)數(shù)听隐,則返回具有相同值(在 Python 浮點(diǎn)精度范圍內(nèi))的浮點(diǎn)數(shù)。如果實(shí)參在 Python 浮點(diǎn)精度范圍外哄啄,則會(huì)觸發(fā)?OverflowError雅任。
對(duì)于一般的 Python 對(duì)象?x?风范,?float(x)?指派給?x.__float__()?。
如果沒有實(shí)參椿访,則返回?0.0?乌企。