isinstance

How to find/detect if a build-in function is used in Python AST?

只谈情不闲聊 提交于 2019-12-08 11:28:12
问题 The goal is to detect if a builtin function such as eval() is used in some code. def foo(a): eval('a = 2') I have tried the following approach: ex_ast = ast.parse(inspect.getsource(foo)) for node in ast.walk(ex_ast): if isinstance(node, ast.FunctionDef): print(node.name) The function name foo is printed as the output. I know that Builtin functions don't have constructors. They are in the type Module. So 1 approach would be using types.FunctionType in an isinstance call. But since I'm using

Making 'isinstance' work with decorators

只谈情不闲聊 提交于 2019-12-08 02:53:36
问题 How does the Python isinstance function work internally? Is there anything I can do to alter its results, like define a special function inside a class or something? Here's my use case: class Decorator: def __init__(self, decorated): self._decorated = decorated def __call__(self): return self._decorated() @Decorator class Foo: pass f = Foo() # How can I make this be true? isinstance(f, Foo) Decorator acts almost like a mixin, except a mixing wouldn't be appropriate here. Is there any way I

How to check a specific type of tuple or list?

删除回忆录丶 提交于 2019-12-07 22:08:59
问题 Suppose, var = ('x', 3) How to check if a variable is a tuple with only two elements, first being a type str and the other a type int in python? Can we do this using only one check? I want to avoid this - if isinstance(var, tuple): if isinstance (var[0], str) and (var[1], int): return True return False 回答1: Here's a simple one-liner: isinstance(v, tuple) and list(map(type, v)) == [str, int] Try it out: >>> def check(v): return isinstance(v, tuple) and list(map(type, v)) == [str, int] ... >>>

How to check a specific type of tuple or list?

走远了吗. 提交于 2019-12-06 09:42:00
Suppose, var = ('x', 3) How to check if a variable is a tuple with only two elements, first being a type str and the other a type int in python? Can we do this using only one check? I want to avoid this - if isinstance(var, tuple): if isinstance (var[0], str) and (var[1], int): return True return False Here's a simple one-liner: isinstance(v, tuple) and list(map(type, v)) == [str, int] Try it out: >>> def check(v): return isinstance(v, tuple) and list(map(type, v)) == [str, int] ... >>> check(0) False >>> check(('x', 3, 4)) False >>> check((3, 4)) False >>> check(['x', 3]) False >>> check(('x'

PHP check for instance of DateTime?

扶醉桌前 提交于 2019-12-05 08:21:12
问题 Is this the only way to check if an object is an instance of a class, in my case of the DateTime class? $cls = ReflectionClass("DateTime"); if (! $cls->isInstance( (object) $var ) ) { // is not an instance } It seems a bit heavy to me. 回答1: You could try instanceof­Docs... if ($var instanceof DateTime) { // true } See also is_a­Docs: if (is_a($var, 'DateTime')) { // true } 回答2: if ($var instanceof DateTime) 回答3: You can use get_class function like this: <?php $a = new DateTime(); if (get

“Protocols cannot be used with isinstance()” - why not?

﹥>﹥吖頭↗ 提交于 2019-12-04 17:53:30
问题 The new typing module contains several objects with names like "SupportsInt" (-Float, -Bytes, etc.). The name, and the descriptions on the documentation page for the module, might be read to suggest that you can test whether an object is of a type that "supports __int__() ". But if you try to use isinstance() , it gives a response that makes it clear that that isn't something you are meant to do: >>> isinstance(5, typing.SupportsInt) (Traceback omitted) TypeError: Protocols cannot be used

“Protocols cannot be used with isinstance()” - why not?

半腔热情 提交于 2019-12-03 11:52:22
The new typing module contains several objects with names like "SupportsInt" (-Float, -Bytes, etc.). The name, and the descriptions on the documentation page for the module , might be read to suggest that you can test whether an object is of a type that "supports __int__() ". But if you try to use isinstance() , it gives a response that makes it clear that that isn't something you are meant to do: >>> isinstance(5, typing.SupportsInt) (Traceback omitted) TypeError: Protocols cannot be used with isinstance(). On the other hand, you can use issubclass() : >>> issubclass((5).__class__, typing

Python check if isinstance any type in list?

爷,独闯天下 提交于 2019-12-03 10:27:08
问题 How do I pythonicly do: var = 7.0 var_is_good = isinstance(var, classinfo1) or isinstance(var, classinfo2) or isinstance(var, classinfo3) or ... or isinstance(var, classinfoN) It seems silly I can't just pass in a list of classinfo's: var_is_good = isinstanceofany( var, [classinfo1, classinfo2, ... , classinfoN] ) So what is the isinstanceofany function? 回答1: isinstance() takes a tuple of classes for the second argument. It'll return true if the first argument is an instance of any of the

Python check if isinstance any type in list?

拜拜、爱过 提交于 2019-12-03 04:10:00
How do I pythonicly do: var = 7.0 var_is_good = isinstance(var, classinfo1) or isinstance(var, classinfo2) or isinstance(var, classinfo3) or ... or isinstance(var, classinfoN) It seems silly I can't just pass in a list of classinfo's: var_is_good = isinstanceofany( var, [classinfo1, classinfo2, ... , classinfoN] ) So what is the isinstanceofany function? isinstance() takes a tuple of classes for the second argument. It'll return true if the first argument is an instance of any of the types in that sequence: isinstance(var, (classinfo1, classinfo2, classinfo3)) In other words, isinstance()

Checking if an annotation is of a specific type

怎甘沉沦 提交于 2019-12-02 19:16:26
I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namespace changes in the future, this could cause subtle errors. I would like to do: if(Class.forName(annotation.annotationType().getName()).isInstance( new javax.validation.Valid() )) { ... } But javax.validation.Valid is an abstract class and cannot be instantiated. Is