python magic methods built-in functions

Magic Methods

Magic Methods Called Explanation
__new__(cls [,...]) instance = MyClass(arg1, arg2) __new__ is called when instance is created
__init__(self [,...]) instance = MyClass(arg1, arg2) __init__ is called when instance is created
__cmp__(self, other) self == other, self > other, 等。 Call when comparing
__pos__(self) +self Unary plus operator
__neg__(self) -self Unary minus operator
__invert__(self) ~self Negation operator
__index__(self) x[self] When an object is used as an index used
__nonzero__(self) bool(self) Boolean value of the object
__getattr__(self, name) self.name # name does not exist When accessing a nonexistent property
__setattr__(self, name, val) self.name = val For when a property assignment
__delattr__(self, name) of the self.name When delete a property
__getattribute(self, name) self.name When access to any property
__getitem__(self, key) self[key] When you use an index to access elements
__setitem__(self, key, val) self[key] = val When assigning an index value
__delitem__(self, key) del self[key] When delete an index value
__iter__(self) for x in self Iteration
__contains__(self, value) value in self, value not in self When using the test operation in relation
__concat__(self, value) self + other When connecting two objects
__call__(self [,...]) self(args) When the "call" Object
__enter__(self) with self as x: with environmental management statement
__exit__(self, exc, val, trace) with self as x: with environmental management statement
__getstate__(self) pickle.dump(pkl_file, self) Serialization
__setstate__(self) data = pickle.load(pkl_file) Serialization

Built-in functions

Built-in functions

abs()

delattr()

hash()

memoryview()

set()

all()

dict()

help()

min()

setattr()

any()

dir()

hex()

next()

slice()

ascii()

divmod()

id()

object()

sorted()

bin()

enumerate()

input()

oct()

staticmethod()

bool()

eval()

int()

open()

str()

breakpoint()

exec()

isinstance()

ord()

sum()

bytearray()

filter()

issubclass()

pow()

super()

bytes()

float()

iter()

print()

tuple()

callable()

format()

len()

property()

type()

chr()

frozenset()

list()

range()

vars()

classmethod()

getattr()

locals()

repr()

zip()

compile()

globals()

map()

reversed()

__import__()

complex()

hasattr()

max()

round()

Published 105 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/x1131230123/article/details/104510454