isinstance

isinstance not working correctly with beautifulsoup(NameError)

杀马特。学长 韩版系。学妹 提交于 2019-12-25 16:58:14
问题 I'm using isinstance to select some html tags and passing them to a Beautifulsoup function. The problem is I keep getting NameErrors from what should be perfectly executable code. def horse_search(tag): return (tag.has_attr('href') and isinstance(tag.previous_element, span)) ... for tag in soup.find_all(horse_search): print (tag) NameError: global name 'span' is not defined Also I'm getting errors from the example code in the documentation of Beautifulsoup using isinstance in conjunction with

Python 3 isinstance unexpected behavior when importing class from different file?

。_饼干妹妹 提交于 2019-12-23 15:38:39
问题 I am trying to import a class from one file and check if is an instance of that class in the file that it was defined in. The problem is that instead of returning True from the isinstance() function, it returns False , because it was initialised in a different file. Here is a working example. Say you have file1.py : class Foo: def __init__(self, arg1): self.arg1 = arg1 def main(class_obj): # Prints false and is type <class 'file1.Foo'> print(type(class_obj)) print(isinstance(class_obj, Foo))

Checking if an annotation is of a specific type

为君一笑 提交于 2019-12-20 09:37:41
问题 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

issubclass of abstract base class Sequence

余生颓废 提交于 2019-12-18 08:57:30
问题 This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__ , __len__ , __contains__ , __iter__ , __reversed__ , index , and count . So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self

python isinstance vs hasattr vs try/except: What is better?

懵懂的女人 提交于 2019-12-17 19:14:27
问题 I am trying to figure out the tradeoffs between different approaches of determining whether or not with object obj you can perform action do_stuff() . As I understand, there are three ways of determining if this is possible: # Way 1 if isinstance(obj, Foo): obj.do_stuff() # Way 2 if hasattr(obj, 'do_stuff'): obj.do_stuff() # Way 3 try: obj.do_stuff() except: print 'Do something else' Which is the preferred method (and why)? 回答1: I believe that the last method is generally preferred by Python

Comparing boolean and int using isinstance

僤鯓⒐⒋嵵緔 提交于 2019-12-17 07:43:08
问题 Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code. print isinstance(True, (float, int)) True My guess would be that its Python's internal subclassing, as zero and one - whether float or int - both evaluate when used as boolean, but don't know the exact reason. What would be the most pythonic way to solve such a situation? I could use type() but in most cases this is considered less pythonic. 回答1: For historic

Comparing boolean and int using isinstance

女生的网名这么多〃 提交于 2019-12-17 07:42:02
问题 Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code. print isinstance(True, (float, int)) True My guess would be that its Python's internal subclassing, as zero and one - whether float or int - both evaluate when used as boolean, but don't know the exact reason. What would be the most pythonic way to solve such a situation? I could use type() but in most cases this is considered less pythonic. 回答1: For historic

Python: checking type of variables

大憨熊 提交于 2019-12-13 02:24:29
问题 I have a question about python. I have variables a , b , c and d . And I have the following line: if not isinstance(a, int) or not isinstance(b, int) \ or not isinstance(c, int) or not isinstance(d, int) \ or not isinstance(a, float) or not isinstance(b, float)\ or not isinstance(c, float) or not isinstance(d, float): do something Is it possible to make this code shorter? Thanks! 回答1: U should use all : if not all(isinstance(var, (int, float)) for var in [a, b, c, d]): # do stuff Note, that

instanceof vs isInstance()

六眼飞鱼酱① 提交于 2019-12-12 11:07:44
问题 class A{ public A(){ System.out.println("in A"); } } public class SampleClass{ public static void main(String[] args) { A a = new A(); System.out.println(A.class.isInstance(a.getClass())); } } Output: false Why is it false? Both A.class and a.getClass() should not return the same class! And in which condition we will get true from the isInstance() method? 回答1: Because a.getClass() returns Class<A> , but you should pass in an A: System.out.println(A.class.isInstance(a)); If you have two Class

check type within numpy array

心不动则不痛 提交于 2019-12-10 14:59:40
问题 I have different types of data. most of them are int and sometimes float . The int is different in size so 8/ 16/ 32 bits are the sizes. For this situation I'm creating a numerical type converter. therefore i check the type by using isinstence() . This because I have read that isinstance() is less worse than type() . The point is that a lot of data i get is numpy arrays. I use spyder as IDE and then i see by the variables also a type. but when i type isinstance(var,'type i read') i get False