python-datamodel

Looping over a Python / IronPython Object Methods

こ雲淡風輕ζ 提交于 2019-12-01 21:28:35
问题 What is the proper way to loop over a Python object's methods and call them? Given the object: class SomeTest(): def something1(self): print "something 1" def something2(self): print "something 2" 回答1: You can use the inspect module to get class (or instance) members: >>> class C(object): ... a = 'blah' ... def b(self): ... pass ... ... >>> c = C() >>> inspect.getmembers(c, inspect.ismethod) [('b', <bound method C.b of <__main__.C object at 0x100498250>>)] getmembers() returns a list of

Looping over a Python / IronPython Object Methods

﹥>﹥吖頭↗ 提交于 2019-12-01 19:38:49
What is the proper way to loop over a Python object's methods and call them? Given the object: class SomeTest(): def something1(self): print "something 1" def something2(self): print "something 2" You can use the inspect module to get class (or instance) members: >>> class C(object): ... a = 'blah' ... def b(self): ... pass ... ... >>> c = C() >>> inspect.getmembers(c, inspect.ismethod) [('b', <bound method C.b of <__main__.C object at 0x100498250>>)] getmembers() returns a list of tuples, where each tuple is (name, member). The second argument to getmembers() is the predicate, which filters

How would you determine where each property and method of a Python class is defined?

一笑奈何 提交于 2019-12-01 08:56:39
Given an instance of some class in Python, it would be useful to be able to determine which line of source code defined each method and property (e.g. to implement 1 ). For example, given a module ab.py class A(object): z = 1 q = 2 def y(self): pass def x(self): pass class B(A): q = 4 def x(self): pass def w(self): pass define a function whither(class_, attribute) returning a tuple containing the filename, class, and line in the source code that defined or subclassed attribute . This means the definition in the class body, not the latest assignment due to overeager dynamism. It's fine if it

When where and how can i change the __class__ attr of an object?

最后都变了- 提交于 2019-11-30 07:24:57
问题 I'd like to be able to do: >>> class a(str): ... pass ... >>> b = a() >>> b.__class__ = str Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __class__ assignment: only for heap types 回答1: I've solved it in this way: >>> class C(str): ... def __getattribute__(self, name): ... if name == '__class__': ... return str ... else: ... return super(C, self).__getattribute__(name) ... >>> c = C() >>> c.__class__ <type 'str'> 回答2: Python 2 doesn't have a unified object

How do you check whether a python method is bound or not?

ⅰ亾dé卋堺 提交于 2019-11-30 06:54:40
问题 Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to? 回答1: def isbound(method): return method.im_self is not None def instance(bounded_method): return bounded_method.im_self User-defined methods: When a user-defined method object is created by retrieving a user-defined function object from a class, its im_self attribute is None and the method object is said to be unbound. When one is created

What is the relationship between the Python data model and built-in functions?

♀尐吖头ヾ 提交于 2019-11-29 22:14:45
As I read Python answers on Stack Overflow, I continue to see some people telling users to use the data model's special methods or attributes directly. I then see contradicting advice (sometimes from myself) saying not to do that, and instead to use builtin functions and the operators directly. Why is that? What is the relationship between the special "dunder" methods and attributes of the Python data model and builtin functions ? When am I supposed to use the special names? What is the relationship between the Python datamodel and builtin functions? The builtins and operators use the

When where and how can i change the __class__ attr of an object?

浪子不回头ぞ 提交于 2019-11-29 03:26:17
I'd like to be able to do: >>> class a(str): ... pass ... >>> b = a() >>> b.__class__ = str Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __class__ assignment: only for heap types Juanjo Conti I've solved it in this way: >>> class C(str): ... def __getattribute__(self, name): ... if name == '__class__': ... return str ... else: ... return super(C, self).__getattribute__(name) ... >>> c = C() >>> c.__class__ <type 'str'> Chris Warburton Python 2 doesn't have a unified object hierarchy (ie. not everything is descended from the object class). Anything that is

How do you check whether a python method is bound or not?

拜拜、爱过 提交于 2019-11-28 22:31:08
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to? def isbound(method): return method.im_self is not None def instance(bounded_method): return bounded_method.im_self User-defined methods: When a user-defined method object is created by retrieving a user-defined function object from a class, its im_self attribute is None and the method object is said to be unbound. When one is created by retrieving a user-defined function object from a class via one of its instances, its im_self attribute

What is the relationship between the Python data model and built-in functions?

别来无恙 提交于 2019-11-28 18:44:50
问题 As I read Python answers on Stack Overflow, I continue to see some people telling users to use the data model's special methods or attributes directly. I then see contradicting advice (sometimes from myself) saying not to do that, and instead to use builtin functions and the operators directly. Why is that? What is the relationship between the special "dunder" methods and attributes of the Python data model and builtin functions? When am I supposed to use the special names? 回答1: What is the

How python attribute lookup process works?

南笙酒味 提交于 2019-11-28 18:42:38
When i say "python attribute lookup proccess" i mean: what python does when you write x.foo?? Searching the web i didn't found to much docs about this, one of the best papers i found resumed the proccess to the following steps (you can see the full article here ) If attrname is a special (i.e. Python-provided) attribute for objectname, return it. Check objectname.__class__.__dict__ for attrname. If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname.__class__ for the same case. Check objectname.__dict__ for attrname, and return if found. If