"The Python interpreter has built-in functions and types that you can use at any time. This chapter introduces 10 functions that start with the letters h,i. "
-
hasattr()
-
hash()
-
help()
-
hex()
-
id()
-
input()
-
int()
-
isinstance()
-
issubclass()
-
iter()
hasattr
( object , name )The arguments are an object and a string. Returns True if the string is the name of one of the object's properties, False otherwise. (This function is implemented by calling getattr(object, name) to see if there is an AttributeError exception.)
hash
( object )Returns the hash of this object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys when looking up elements in the dictionary. Numeric variables of the same size have the same hash value (even if they are of different types, such as 1 and 1.0).
help
( [ object ] )Starts the built-in help system (this function is mainly used interactively). If there are no arguments, an interactive help system is launched in the interpreter console. If the argument is a string, searches for the string in a module, function, class, method, keyword, or documentation topic and prints help information on the console. If the argument is any other object, a help page for that object is generated.
hex
( x )Convert an integer to a lowercase hex string prefixed with "0x". If x is not a Python int object, an __index__() method that returns an integer must be defined.
1) > hex(
'0x1'
15) > hex(
'0xf'
255) > hex(
'0xff'
45) > hex(-
'-0x2d'
If you want to get the hexadecimal string form of a floating point number, use the float.hex() method.
id
( object )Returns the "identity value" of the object. The value is an integer guaranteed to be unique and constant over the lifetime of this object. Two objects with non-overlapping lifetimes may have the same id() value.
1 > x =
> id(x)
140715753281184
input
( [ prompt ] )If the prompt argument is present, it is written to standard output without a trailing newline. Next, the function reads a line from the input, converts it to a string (except for the trailing newline), and returns. When EOF is read, EOFError is triggered.
"type your name: ") > name = input(
type your name: alex
> name
int
( [ x ] )class int
( x , base=10 )
Returns an integer object constructed from a number or string x, or 0 if no arguments are given. If x defines __int__(), int(x) returns x.__int__(). If x defines __index__(), it will return x.__index__(). If x defines __trunc__(), it will return x.__trunc__(). For floating point numbers, it will round towards zero.
If x is not a number, or has a base parameter, x must be a string, bytes, or a bytearray instance representing an integer literal in base. The text can be preceded by + or - (no spaces in between) and can be preceded and followed by spaces. A number in base n contains numbers from 0 to n-1, where a to z (or A to Z) represent 10 to 35. The default base is 10, and the allowed bases are 0, 2-36. 2, 8, and hexadecimal numbers can be represented by 0b/0B, 0o/0O, 0x/0X prefixes in the code. A base of 0 will be interpreted exactly as the code literal, and the final result will be one of 2, 8, 10, or hexadecimal. So int('010', 0) is illegal, but int('010') and int('010', 8) are legal.
isinstance
( object , classinfo )Returns True if the parameter object is an instance of the parameter classinfo or its (direct, indirect or virtual) subclass. If object is not an object of the given type, the function will always return False. If classinfo is a tuple of type objects (or a tuple recursively composed of other such tuples), then returns True if object is an instance of any of these types. If classinfo is neither a type nor a tuple of type or a tuple of type tuples, a TypeError exception will be raised.
issubclass
( class , classinfo )Returns True if class is a (direct, indirect or virtual) subclass of classinfo. A class is considered a subclass of itself. classinfo can also be a tuple of class objects, in which case each entry in classinfo will be checked. In any other case, a TypeError exception will be raised.
iter
( object [ , sentinel ] )Returns an iterator object. The interpretation of the first argument is very different depending on whether there is a second argument. Without the second argument, object must be a collection object that supports the iteration protocol (with the __iter__() method), or must support the sequence protocol (with the __getitem__() method, and numeric arguments start at 0). If it does not support these protocols, a TypeError will be raised. If there is a second argument sentinel, then object must be a callable object. The iterator generated in this case will call object without arguments each time its __next__() method is called iteratively; if the returned result is sentinel, StopIteration will be triggered, otherwise the call result will be returned.