函數 | 描述 |
---|---|
setattr(object, name, value) | 設定物件 object 的新屬性 |
舉例示範如下
class Demo: def __init__(self, i): self.i = i self.x = "xxx" self.y = "yyy" self.z = "zzz" def __str__(self): return str(self.i) def hello(self): print("hello " + self.__str__()) d = Demo(22) print(dir(d)) setattr(d, "a", 0) print(dir(d)) setattr(d, "b", 1) print(dir(d)) setattr(d, "c", 2) print(dir(d)) # 《程式語言教學誌》的範例程式 # http://pydoing.blogspot.com/ # 檔名:setattr.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月
執行結果如下
中英文術語對照 | |
---|---|
函數 | function |
參數 | parameter |
內建函數
- abs(x)
- all(iterable)
- any(iterable)
- ascii(object)
- bin(x)
- bool([x])
- bytearray([source[, encoding[, errors]]])
- bytes([source[, encoding[, errors]]])
- chr(i)
- classmethod(function)
- compile(source, filename, mode, flags=0, dont_inherit=False)
- complex([real[, imag]])
- delattr(object, name)
- dict([arg])
- dir([object])
- divmod(a, b)
- enumerate(iterable, start=0))
- eval(expression, globals=None, locals=None)
- exec(object[, globals[, locals]])
- filter(function, iterable)
- float([x])
- format(value[, format_spec])
- frozenset([iterable])
- getattr(object, name[, default])
- globals()
- hasattr(object, name)
- hash(object)
- help([object])
- hex(x)
- id(object)
- input([prompt])
- int([number | string[, base]])
- isinstance(object, classinfo)
- issubclass(class, classinfo)
- iter(object[, sentinel])
- len(s)
- list([iterable])
- locals()
- map(function, iterable, ...)
- max(iterable[, args...], *[, key])
- memoryview(obj)
- min(iterable[, args...], *[, key])
- next(iterator[, default])
- object()
- oct(x)
- open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
- ord(c)
- pow(x, y[, z])
- print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
- property(fget=None, fset=None, fdel=None, doc=None)
- range([start], stop[, step])
- repr(object)
- reversed(seq)
- round(x[, n])
- set([iterable])
- setattr(object, name, value)
- slice([start], stop[, step])
- sorted(iterable[, key][, reverse])
- staticmethod(function)
- str([object[, encoding[, errors]]])
- sum(iterable[, start])
- super([type[, object-or-type]])
- tuple([iterable])
- type(object)
- vars([object])
- zip(*iterables)
沒有留言:
張貼留言