统计

留言簿(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 阅读(243) 评论(0)  编辑  收藏 所属分类: Python


只有注册用户登录后才能发表评论。


网站导航: