python内置方法、模块讲解(二十二)

  • 2016-05-23 20:47:04
  • 1188
  • 0

六十、print方法

此方法不做多讲解,python的打印函数

六十一、property类

1、定义:

class property(object)
 |  property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
 |
 |  fget is a function to be used for getting an attribute value, and likewise
 |  fset is a function for setting, and fdel a function for del'ing, an
 |  attribute.  Typical use is to define a managed attribute x:
 |
 |  class C(object):
 |      def getx(self): return self._x
 |      def setx(self, value): self._x = value
 |      def delx(self): del self._x
 |      x = property(getx, setx, delx, "I'm the 'x' property.")
 |
 |  Decorators make defining new properties or modifying existing ones easy:
 |
 |  class C(object):
 |      @property
 |      def x(self):
 |          "I am the 'x' property."
 |          return self._x
 |      @x.setter
 |      def x(self, value):
 |          self._x = value
 |      @x.deleter
 |      def x(self):
 |          del self._x

2、解释:这个类用法稍微有点难理解,但是通过定义中的例子还是可以理解的。它有两种用法(目的都是定义一个托管性的属性):一种是函数方式,传入参数fget、fset、fdel为方法,doc为字符串(不传入时为None);另一种是以装饰器的方式。 

3、举例:(见定义)

六十二、quit方法

比较少用,一般用于python环境的退出(略)

六十三、range方法

1、定义:

range(...)
    range(stop) -> list of integers
    range(start, stop[, step]) -> list of integers

    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

2、解释:返回一个由连续的等差数字组成的列表

3、举例:

>>> range(1,10,3)
[1, 4, 7]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
 


发表评论

* *