導航

          <2011年7月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          隨筆分類

          隨筆檔案

          統計

          留言簿(1)

          DB

          Others

          QA

          Tech Website

          閱讀排行榜

          評論排行榜

          Special Methods for Customizing Classes

          Table 13.4. Special Methods for Customizing Classes

          Special Method

          Description

          Basic Customization

          C.__init__(self[, arg1, ...] )

          Constructor (with any optional arguments)

          C.__new__(self[, arg1, ...] )[a]

          Constructor (with any optional argu ments); usually used for setting up subclassing of immutable data types

          C.__del__(self)

          Destructor

          C.__str__(self)

          Printable string representation; str() built-in and print statement

          C.__repr__(self)

          Evaluatable string representation; repr() built-in and '' operator

          C.__unicode__(self)[b]

          Unicode string representation; unicode() built-in

          C.__call__(self, *args)

          Denote callable instances

          C.__nonzero__(self)

          Define False value for object; bool() built-in (as of 2.2)

          C.__len__(self)

          "Length" (appropriate for class); len() built-in

          Object (Value) Comparison[c]

          C.__cmp__(self, obj)

          object comparison; cmp() built-in

          C.__lt__(self, obj) and C.__le__(self, obj)

          less than/less than or equal to; < and <= operators

          C.__gt__(self, obj) and C.__ge__(self, obj)

          greater than/greater than or equal to; > and >= operators

          C.__eq__(self, obj) and C.__ne__(self, obj)

          equal/not equal to; ==,!= and <> operators

          Attributes

          C.__getattr__(self, attr)

          Get attribute; getattr() built-in; called only if attributes not found

          C.__setattr__(self, attr, val)

          Set attribute;

          C.__delattr__(self, attr)

          Delete attribute;

          C.__getattribute__(self, attr)[a]

          Get attribute; getattr() built-in; always called

          C.__get__(self, attr)[a]

          (descriptor) Get attribute

          C.__set__(self, attr, val)[a]

          (descriptor) Set attribute

          C.__delete__(self, attr)[a]

          (descriptor) Delete attribute

          Customizing Classes / Emulating Types

          Numeric Types: Binary Operators[d]

          C.__*add__(self, obj)

          Addition; + operator

          C.__*sub__(self, obj)

          Subtraction; - operator

          C.__*mul__(self, obj)

          Multiplication; * operator

          C.__*div__(self, obj)

          Division; / operator

          C.__*truediv__(self, obj)[e]

          True division; / operator

          C.__*floordiv__(self, obj)[e]

          Floor division; // operator

          C.__*mod__(self, obj)

          Modulo/remainder; % operator

          C.__*divmod__(self, obj)

          Division and modulo; divmod() built-in

          C.__*pow__(self, obj[, mod])

          Exponentiation; pow() built-in; ** operator

          C.__*lshift__(self, obj)

          Left shift; << operator

          Customizing Classes / Emulating Types

          Numeric Types: Binary Operators[f]

          C.__*rshift__(self, obj)

          Right shift; >> operator

          C.__*and__(self, obj)

          Bitwise AND; & operator

          C.__*or__(self, obj)

          Bitwise OR; | operator

          C.__*xor__(self, obj)

          Bitwise XOR; ^ operator

          Numeric Types: Unary Operators

          C.__neg__(self)

          Unary negation

          C.__pos__(self)

          Unary no-change

          C.__abs__(self)

          Absolute value; abs() built-in

          C.__invert__(self)

          Bit inversion; ~ operator

          Numeric Types: Numeric Conversion

          C.__complex__(self, com)

          Convert to complex; complex() built-in

          C.__int__(self)

          Convert to int; int() built-in

          C.__long__(self)

          Convert to long; long() built-in

          C.__float__(self)

          Convert to float; float() built-in

          Numeric Types: Base Representation (String)

          C.__oct__(self)

          Octal representation; oct() built-in

          C.__hex__(self)

          Hexadecimal representation; hex() built-in

          Numeric Types: numeric coercion

          C.__coerce__(self, num)

          Coerce to same numeric type; coerce() built-in

          C.__index__(self)[g]

          Coerce alternate numeric type to integer if/when necessary (e.g., for slice indexes, etc.)

          Sequence Types[e]

          C.__len__(self)

          Number of items in sequence

          C.__getitem__(self, ind)

          Get single sequence element

          C.__setitem__(self, ind, val)

          Set single sequence element

          C.__delitem__(self, ind)

          Delete single sequence element

          Special Method

          Description

          Sequence Types[e]

          C.__getslice__(self, ind1, ind2)

          Get sequence slice

          C.__setslice__(self, i1, i2, val)

          Set sequence slice

          C.__delslice__(self, ind1, ind2)

          Delete sequence slice

          C.__contains__(self, val)[f]

          Test sequence membership; in keyword

          C.__*add__(self, obj)

          Concatenation; + operator

          C.__*mul__(self, obj)

          Repetition; * operator

          C.__iter__(self)[e]

          Create iterator class; iter() built-in

          Mapping Types

          C.__len__(self)

          Number of items in mapping

          C.__hash__(self)

          Hash function value

          C.__getitem__(self, key)

          Get value with given key

          C.__setitem__(self, key, val)

          Set value with given key

          C.__delitem__(self, key)

          Delete value with given key

          C.__missing__(self, key)[g]

          Provides default value when dictionary does not have given key

          posted on 2011-07-21 23:25 XXXXXX 閱讀(260) 評論(0)  編輯  收藏 所屬分類: Python

          主站蜘蛛池模板: 高尔夫| 丹阳市| 望奎县| 洛宁县| 祥云县| 页游| 澳门| 任丘市| 兴宁市| 钦州市| 雅江县| 白朗县| 石屏县| 康定县| 花莲市| 天柱县| 开平市| 溧阳市| 九龙城区| 玛沁县| 瓮安县| 乐安县| 巍山| 天长市| 岑巩县| 土默特左旗| 望江县| 积石山| 张家港市| 都匀市| 大宁县| 泰和县| 武威市| 霸州市| 宁都县| 襄樊市| 南宁市| 成都市| 华池县| 秦安县| 南江县|