Python 函数

来源:互联网 发布:highcharts zh ch.js 编辑:程序博客网 时间:2024/05/23 23:58
1. ",".join()
>>> ",".join(["a", "b", "c"])
'a,b,c'

The "," is inserted between each element of the given list. In your case, your "list" is the string representation "595", which is treated as the list ["5", "9", "5"].


2. SringIO

   7.5. StringIO — Read and write strings as files

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects). (For standard strings, see str and unicode.)

class StringIO.StringIO([buffer])

When a StringIO object is created, it can be initialized to an existing string by passing the string to the constructor. If no string is given, the StringIOwill start empty. In both cases, the initial file position starts at zero.

The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. If both are used, 8-bit strings that cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause a UnicodeError to be raised when getvalue() is called.

The following methods of StringIO objects require special mention:

StringIO.getvalue()

Retrieve the entire contents of the “file” at any time before the StringIO object’s close() method is called. See the note above for information about mixing Unicode and 8-bit strings; such mixing can cause this method to raise UnicodeError.

StringIO.close()

Free the memory buffer. Attempting to do further operations with a closed StringIO object will raise a ValueError.

Example usage:

import StringIOoutput = StringIO.StringIO()output.write('First line.\n')print >>output, 'Second line.'# Retrieve file contents -- this will be# 'First line.\nSecond line.\n'contents = output.getvalue()# Close object and discard memory buffer --# .getvalue() will now raise an exception.output.close()



3. 类的有关概念

---从模块中引用命名是引用属性:表达式 modname.funcname中,modname 是一个模块对象,funcname 是它的一个属性。

----

class MyClass:    """A simple example class"""    i = 12345    def f(self):        return 'hello world'

那么 MyClass.i 和 MyClass.f 是有效的属性引用,分别返回一个整数和一个方法对象。

---另一种为实例对象所接受的引用属性是 方法 。方法是“属于”一个对象的函数。

x.f 是一个有效的方法引用,因为 MyClass.f 是一个函数。不过 x.f 和 MyClass.f 不同,它是一个 方法对象,不是一个函数对象。

如果这个命名确认为一个有效的函数对象类属性,就会将实例(类定义)对象和函数对象封装进一个抽象对象:这就是方法对象。

0 0