python: built-in method cont1.


                                                                13.list([sequence])Return a list whose items are the same and in the same order as sequence's items. sequence may be either a sequence, a container that supports iteration, or an iterator object. If sequence is already a list, a copy is made and returned, similar to sequence[:]. For instance, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new empty list, [].>>> list((1,2,4,5))[1, 2, 4, 5]>>> list()[]
dict([mapping-or-sequence])Return a new dictionary initialized from an optional positional argument or from a set of keyword arguments. If no arguments are given, return a new empty dictionary. If the positional argument is a mapping object, return a dictionary mapping the same keys to the same values as does the mapping object. Otherwise the positional argument must be a sequence, a container that supports iteration, or an iterator object. The elements of the argument must each also be of one of those kinds, and each must in turn contain exactly two objects. The first is used as a key in the new dictionary, and the second as the key's value. If a given key is seen more than once, the last value associated with it is retained in the new dictionary.If keyword arguments are given, the keywords themselves with their associated values are added as items to the dictionary. If a key is specified both in the positional argument and as a keyword argument, the value associated with the keyword is retained in the dictionary. For example, these all return a dictionary equal to {"one": 2, "two": 3}:
>>> dict({'one': 2, 'two': 3}){'two': 3, 'one': 2}>>> dict({'one': 2, 'two': 3}.items()){'two': 3, 'one': 2}>>> dict({'one': 2, 'two': 3}.iteritems()){'two': 3, 'one': 2}>>> dict(zip(('one', 'two'), (2, 3))){'two': 3, 'one': 2}>>> dict([['two', 3], ['one', 2]]){'two': 3, 'one': 2}>>> dict(one=2, two=3){'two': 3, 'one': 2}>>> dict([(['one', 'two'][i-2], i) for i in (2, 3)]){'two': 3, 'one': 2}>>> dict([('one',2),('two',3)]){'two': 3, 'one': 2}
New in version 2.2. Changed in version 2.3: Support for building a dictionary from keyword arguments added.
tuple([sequence])Return a tuple whose items are the same and in the same order as sequence's items. sequence may be a sequence, a container that supports iteration, or an iterator object. If sequence is already a tuple, it is returned unchanged. For instance, tuple('abc') returns ('a', 'b', 'c') and tuple([1, 2, 3]) returns (1, 2, 3). If no argument is given, returns a new empty tuple, ().>>> tuple("abc")('a', 'b', 'c')>>> tuple([1,2,3])(1, 2, 3)>>> tuple()()
set([iterable])Return a set whose elements are taken from iterable. The elements must be immutable. To represent sets of sets, the inner sets should be frozenset objects. If iterable is not specified, returns a new empty set, set([]). New in version 2.4.>>> set('ycl')set(['y', 'c', 'l'])>>> set([9,8,7])set([8, 9, 7])>>> set()set([])
                                                      
               
               
                14. file(filename[, mode[, bufsize]])Return a new file object (described in section 
2.3.9
, ``
File Objects
''). The first two arguments are the same as for stdio's fopen(): filename is the file name to be opened, mode indicates how the file is to be opened: 'r' for reading, 'w' for writing (truncating an existing file), and 'a' opens it for appending (which on some Unix systems means that all writes append to the end of the file, regardless of the current seek position).Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files (else it is ignored). If the file cannot be opened, IOError is raised.
In addition to the standard fopen() values mode may be 'U' or 'rU'. If Python is built with universal newline support (the default) the file is opened as a text file, but lines may be terminated by any of '\n', the Unix end-of-line convention, '\r', the Macintosh convention or '\r\n', the Windows convention. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newline supportmode 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen.
If mode is omitted, it defaults to 'r'. When opening a binary file, you should append 'b' to the mode value for improved portability. (It's useful even on systems which don't treat binary and text files differently, where it serves as documentation.) The optional bufsize argument specifies the file's desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative bufsize means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.
2.3
The file() constructor is new in Python 2.2. The previous spelling, open(), is retained for compatibility, and is an alias for file().
open(filename[, mode[, bufsize]])An alias for the file() function above.
15.globals()Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
locals()Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. 
16. getattr(object, name[, default])Return the value of the named attributed of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
setattr(object, name, value)This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.
               
               
                >>> class A:
    def __init__(self):
        self.x = 3
>>> a = A()
>>> getattr(a, x)
Traceback (most recent call last):
  File "", line 1, in module>
    getattr(a, x)
NameError: name 'x' is not defined
>>> getattr(a, 'x')
3
>>> setattr(a,'x',18)
>>> print a.x
18
17. sum(sequence[, start])Sums start and the items of a sequence, from left to right, and returns the total. start defaults to 0. The sequence's items are normally numbers, and are not allowed to be strings. The fast, correct way to concatenate sequence of strings is by calling ''.join(sequence). Note that sum(range(n), m) is equivalent to reduce(operator.add, range(n), m) New in version 2.3.
>>> sum([1,2,4,5])
12
>>> sum([1,2,4,5],2)
14
18.repr(object)Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval().str([object])Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
               
               
                unicode([object[, encoding [, errors]]])Return the Unicode string version of object using one of the following modes:If encoding and/or errors are given, unicode() will decode the object which can either be an 8-bit string or a character buffer using the codec for encoding. The encoding parameter is a string giving the name of an encoding; if the encoding is not known, LookupError is raised. Error handling is done according to errors; this specifies the treatment of characters which are invalid in the input encoding. If errors is 'strict' (the default), a ValueError is raised on errors, while a value of 'ignore' causes errors to be silently ignored, and a value of 'replace' causes the official Unicode replacement character, U+FFFD, to be used to replace input characters which cannot be decoded. See also the 
codecs
 module.
If no optional parameters are given, unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
For objects which provide a __unicode__() method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in 'strict' mode.
New in version 2.0. Changed in version 2.2: Support for __unicode__() added.
>>> class A:
    def __init__(self):
        self.x = 3
>>> a = A()
>>> print a
__main__.A instance at 0x01602418>
>>> print str(a)
__main__.A instance at 0x01602418>
>>> print repr(a)
__main__.A instance at 0x01602418>
>>> print '%s'%a
__main__.A instance at 0x01602418>
>>> a + 'yang'
Traceback (most recent call last):
  File "", line 1, in module>
    a + 'yang'
TypeError: unsupported operand type(s) for +: 'instance' and 'str'
>>> str(a) + "yang"
'yang'
>>> repr(a)+'lin'
'lin'