python-datamodel

List of all Python dunder methods - Which ones do you need to implement to correctly proxy an object?

ε祈祈猫儿з 提交于 2020-08-23 03:30:12
问题 I'm trying to create an object proxy. Attribute/property lookup can be done by simply implementing the __getattribute__ , __setattr__ and __delattr__ methods. However, other functionalities like len(x), x[], bool(x) require other dunder methods like __len__, __getitem__, __bool__ to be implemented. If you don't implement these on the proxy class, but the object you're proxying supports them, your proxy will be incomplete and cause runtime errors. I would therefore like to have a comprehensive

Why doesn't Python have a “__req__” (reflected equality) method?

心不动则不痛 提交于 2020-06-14 06:16:47
问题 I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use a list comprehension (as in np.array(x in (2,3) for x in arr ). (I maintain a UI that lets (trusted) users type in arbitrary code, and a == AnyOf(1,2,3) is a lot more palatable than a list

Why doesn't Python have a “__req__” (reflected equality) method?

浪子不回头ぞ 提交于 2020-06-14 06:16:06
问题 I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use a list comprehension (as in np.array(x in (2,3) for x in arr ). (I maintain a UI that lets (trusted) users type in arbitrary code, and a == AnyOf(1,2,3) is a lot more palatable than a list

Is there any way to tell if a function object was a lambda or a def?

天大地大妈咪最大 提交于 2020-06-08 07:29:25
问题 Consider the two functions below: def f1(): return "potato" f2 = lambda: "potato" f2.__name__ = f2.__qualname__ = "f2" Short of introspecting the original source code, is there any way to detect that f1 was a def and f2 was a lambda? >>> black_magic(f1) "def" >>> black_magic(f2) "lambda" 回答1: You could check the code object's name. Unlike the function's name, the code object's name cannot be reassigned. A lambda's code object's name will still be '<lambda>' : >>> x = lambda: 5 >>> x.__name__

Is everything greater than None?

做~自己de王妃 提交于 2020-01-08 16:22:09
问题 Is there a Python built-in datatype, besides None , for which: >>> not foo > None True where foo is a value of that type? How about Python 3? 回答1: None is always less than any datatype in Python 2 (see object.c). In Python 3, this was changed; now doing comparisons on things without a sensible natural ordering results in a TypeError . From the 3.0 "what's new" updates : Python 3.0 has simplified the rules for ordering comparisons: The ordering comparison operators ( < , <= , >= , > ) raise a

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 10:30:24
问题 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

Setting a class __name__ declaratively

纵然是瞬间 提交于 2019-12-30 09:10:10
问题 Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier? >>> class Potato: ... __name__ = 'not Potato' ... >>> Potato.__name__ # doesn't stick 'Potato' >>> Potato().__name__ # .. but it's in the dict 'not Potato' I thought maybe it was simply a case that this was overwritten after the class definition block completes. But seems that's not true, because the name is writable yet apparently not set in the class dict: >>> Potato.__name__ = 'no

Get class that defined method

*爱你&永不变心* 提交于 2019-12-17 02:31:27
问题 How can I get the class that defined a method in Python? I'd want the following example to print " __main__.FooClass ": class FooClass: def foo_method(self): print "foo" class BarClass(FooClass): pass bar = BarClass() print get_class_that_defined_method(bar.foo_method) 回答1: import inspect def get_class_that_defined_method(meth): for cls in inspect.getmro(meth.im_class): if meth.__name__ in cls.__dict__: return cls return None 回答2: Thanks Sr2222 for pointing out I was missing the point... Here

Get class that defined method

人盡茶涼 提交于 2019-12-17 02:30:17
问题 How can I get the class that defined a method in Python? I'd want the following example to print " __main__.FooClass ": class FooClass: def foo_method(self): print "foo" class BarClass(FooClass): pass bar = BarClass() print get_class_that_defined_method(bar.foo_method) 回答1: import inspect def get_class_that_defined_method(meth): for cls in inspect.getmro(meth.im_class): if meth.__name__ in cls.__dict__: return cls return None 回答2: Thanks Sr2222 for pointing out I was missing the point... Here

Is there a way to access the formal parameters if you implement __getattribute__

最后都变了- 提交于 2019-12-10 17:07:08
问题 It seems as though __getattribute__ has only 2 parameters (self, name) . However, in the actual code, the method I am intercepting actually takes arguments. Is there anyway to access those arguments? Thanks, Charlie 回答1: __getattribute__ simply returns the attribute that was requested, in case of a method, the __call__ interface is then used to call it. Instead of returning the method, return a wrapper around it, for instance: def __getattribute__(self, attr): def make_interceptor(callble):