Python basic function 03 (G ~ I)

The Python interpreter has many built-in functions and types that are always available. They are listed here in alphabetical order.

Built-in Functions Built-in Functions
getattr() globals()
hasattr () hash()
help() hex()
id() input()
int() isinstance()
issubclass() process ()
import()

getattr(object, name[ , default])

Returns the value of the named attribute of object. The name must be a string. If the string is the name of one of the object properties, the result is the value of that property. For example, equivalent to. If the named attribute does not exist, return default (if provided), otherwise raised. getattr (x, 'foobar') x.foobarAttributeError

globals()

Returns a dictionary representing the current global symbol table. This is always the dictionary of the current module (in a function or method, this is the module that defines the module, not the module from which it is called).

hasattr(object, name)

The parameter is an object and a string. The result is True, whether the string is the name of one of the object properties, False if not. (This is achieved by calling and seeing if it raises.) Getattr (object, name) AttributeError

hash(object)

Returns the hash value of the object (if any). The hash value is an integer. They are used to quickly compare dictionary keywords during dictionary lookup. Comparing equal numbers have the same hash value (even if they are of different types, such as 1 and 1.0).

Note For objects with a custom __hash __ () method, please note that it will hash () truncate the return value according to the host's bit width. For more information about __hash __ (), see.

help([object])

Call the built-in help system. (This feature is intended for interactive use.) If no parameters are provided, the interactive help system will start on the interpreter console. If the parameter is a string, use the string as the name of the module, function, class, method, keyword, or document subject, and print the help page on the console. If the parameter is any other type of object, a help page will be generated on that object.

Please note that if a slash (/) appears in the function's parameter list, then help () when calling, indicating that the parameter before the slash is only position information. For more information, please refer to the FAQ entry on the position parameter only.

This function is added to the built-in namespace by the site module.

Changes were made in version 3.4: Changing pydoc and inspect means that the reported callable signatures are now more comprehensive and consistent.

hex(x )

Convert an integer to a lowercase hexadecimal string prefixed with "0x". If x is not a Python int object, you must define a __index __ () method that returns an integer. Some examples:

hex(255)
'0xff'
hex(-42)
'-0x2a'

If you want to convert an integer to a prefixed or unprefixed uppercase or lowercase hexadecimal string, you can use one of the following two methods:

'%#x' % 255, '%x' % 255, '%X' % 255
('0xff', 'ff', 'FF')
format(255, '#x'), format(255, 'x'), format(255, 'X')
('0xff', 'ff', 'FF')
f'{255:#x}', f'{255:x}', f'{255:X}'
('0xff', 'ff', 'FF')

See also format () for more information.

See also int () converts a hexadecimal string to an integer using base 16.

Note To obtain the hexadecimal string representation of a floating-point number, use the float.hex () method.

id(object)

Returns the "identity" of the object. This is an integer that can be guaranteed to be unique and constant during the lifetime of this object. Two objects with non-overlapping life cycles may have the same id () value.

CPython implementation details: This is the address of the object in memory.

input([prompt])

If there is a prompt parameter, it will be written to standard output without trailing newlines. The function then reads a line from the input, converts it to a string (separating trailing newlines), and returns it. When reading EOF, EOFError will be raised. example:


s = input('--> ')  
Monty Python's Flying Circus
s  
"Monty Python's Flying Circus"

If the readline module is loaded, input () will use it to provide detailed line editing and history functions.

Use the parameter to raise the auditing event builtins.inputprompt before reading the input

After builtins.input / result successfully read the input, the use result triggers an audit event.

class int([x])

class int(x, base=10)

Returns an integer object constructed from a number or the string x, or 0 if no parameters are provided. If x defines __int __ (), then int (x) returns x. Int () . If x defines index () , then return x. Index (). If x defines trunc () , then return x. Trunc (). For floating point numbers, it will be truncated to zero.

If x is not a number or a given base, then x must be a string bytes, or a bytearray representing an instance of integer literals in the base base. Optionally, the text can start with or (with no spaces in between) and be surrounded by spaces. The literal text based on n is composed of numbers from 0 to n-1, and the value of to (or to) is 10 to 35. The default base is 10. Allowed values ​​are 0 and 2–36. Base-2, -8 and -16 literals can be optionally prefixed with /, / or / ± azAZ0b0B0o0O0x0X, which is the same as the integer literal in the code. Base 0 means to be completely interpreted as code text, so the actual base is 2, 8, 10, or 16, so it is not legal, but is and. int ('010', 0) int ('010') int ('010', 8)

Integer types are described in numeric types-int, float, complex.

Changed in version 3.4: If base is not an instance of int and the base object has a base .__ index__ method, then call this method to get the integer of base. The previous version base .__ int__ replaced base. Index .

Changed in version 3.6: Allow numbers to be grouped by underscores in code text.

Changed in version 3.7: x is now a position-only parameter.

Changes were made in version 3.8: index () If __int __ () is not defined, return to.

isinstance(object,classinfo )

Returns True if the object parameter is an instance of the CLASSINFO parameter, or a subclass of it (direct, indirect or virtual). If object is not an object of the given type, the function always returns False. If classinfo is a tuple of type object (or recursively other such tuples), True returns if object is an instance of any type. If classinfo is not a type or tuple of this type, TypeError will raise an exception.

issubclass(class,classinfo )

Returns True if the class is a CLASSINFO of a subclass (direct, indirect or virtual). A class is considered a subclass of itself. classinfo can be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, TypeError will raise an exception.

iter(object [,sentinel ] )

Returns an iterator object. According to the existence of the second parameter, the interpretation of the first parameter is very different. If there is no second parameter, the object must be a collection object that supports the iteration protocol (the __iter __ () method), or it must support the sequence protocol ( method 0 of the getitem () integer parameter starting with int). If it does not support any of these protocols, TypeError is raised. If the second parameter sentinel is given, object must be a callable object. The iterator created in this case will call the object with no parameters __next __ () on every call to its method. If the returned value is equal to the sentinel, StopIteration will be increased, otherwise the value will be returned.

See also iterator type.

A useful application of the second form iter () is to build a block reader. For example, reading a fixed-width block from a binary database file until it reaches the end of the file:

from functools import partial
with open('mydata.db', 'rb') as f:
    for block in iter(partial(f.read, 64), b''):
        process_block(block)

import(name,globals = None,locals = None,fromlist =(),level = 0 )

 注意  与importlib.import_module()不同的是,这是一个高级函数在日常的Python编程中并不需要 。

This function is called by the import statement. It can be replaced (by importing the builtins module and assigned to builtins. Import ) to change the semantics of the import statement, but it is strongly discouraged because it is usually easier to use import hooks (see PEP 302) to achieve the same goal This can cause problems with the code that is assumed to be used by the default import implementation. import () is not recommended to use importlib.import_module () directly.

This function imports the module name, possibly using the given global and local variables to determine how to interpret the name in the context of the package. The fromlist gives the names of the objects or subs that should be imported from the given module. The standard implementation does not use its locals parameter at all, but only uses its global parameters to determine the package context of the import statement.

The level specifies whether to use absolute import or relative import. 0 (default) means only absolute import is performed. A positive value of level indicates the number of parent directories to be searched relative to the module calling directory __import __ () (see PEP 328 for details).

When the name variable takes the form of package.module, it usually returns the top-level package (until the name of the first dot), instead of returning the module named by name. However, when a non-empty fromlist parameter is given, the module named by name will be returned.

For example, this statement causes the byte code to be similar to the following code: import spam

spam = __import__('spam', globals(), locals(), [], 0)

This statement causes this call: import spam.ham

spam = __import__('spam.ham', globals(), locals(), [], 0)

Note how __import __ () returns the top-level module here, because this is the object bound to the name through the import statement.

On the other hand, the statement results from spam.ham import eggs, sausage as saus

_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0)
eggs = _temp.eggs
saus = _temp.sausage

Here, the spam.ham module returns __import __ (). From this object, retrieve the names to be imported and assign them to their respective names.

If you just want to import the module by name (probably in the package), use importlib.import_module ().

A change was made in version 3.3: negative values ​​for levels are no longer supported (this also changes the default value to 0).

Published 36 original articles · praised 0 · visits 616

Guess you like

Origin blog.csdn.net/Corollary/article/details/105424886