How to compare type of an object in Python?

Basically I want to do this: obj = 'str' type ( obj ) == string I tried: type ( obj ) == type ( string ) and it didn't work. Also, what about the other types? For example, I couldn't replicate NoneType . isinstance() In your case, isinstance("this is a string", str) will return True . You may also want to read this: http://www.canonical.org/~kragen/isinstance/ isins

如何比较Python中的对象的类型?

基本上我想这样做: obj = 'str' type ( obj ) == string 我试过了: type ( obj ) == type ( string ) 并没有奏效。 另外,其他类型呢? 例如,我无法复制NoneType 。 isinstance() 在你的情况下, isinstance("this is a string", str)将返回True 。 您可能还想阅读:http://www.canonical.org/~kragen/isinstance/ isinstance作品: if isinstance(obj, MyClass): do_foo(obj) 但请记住:如果它看起

python: how to identify if a variable is an array or a scalar

I have a function that takes the argument NBins . I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30] . How can I identify within the function, what the length of NBins is? or said differently, if it is a scalar or a vector? I tried this: >>> N=[2,3,5] >>> P = 5 >>> len(N) 3 >>> len(P) Traceback (most recent call last): F

python:如何识别变量是数组还是标量

我有一个函数需要NBins的参数。 我想用标量50或数组[0, 10, 20, 30] 0,10,20,30]调用此函数。 如何在函数中识别NBins的长度是多少? 或者换句话说,如果它是一个标量或向量? 我试过这个: >>> N=[2,3,5] >>> P = 5 >>> len(N) 3 >>> len(P) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len()

Function parameter types in Python

Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2): # stuff However, you don't actually give the types of those parameters. Also, if I remember, Python is a strongly typed language, as such, it seems like Python shouldn't let you pass in a parameter of a different type than the function creator expected. However, how does Python kno

Python中的函数参数类型

除非我误解了,否则在Python中创建一个函数就像这样工作: def my_func(param1, param2): # stuff 但是,实际上并没有给出这些参数的类型。 另外,如果我还记得,Python是一种强类型语言,因此Python看起来不应该让你传入一个不同于函数创建者期望类型的参数。 但是,Python如何知道函数的用户正在传递适当的类型? 如果程序是错误的类型,程序是否会死亡,假设函数实际使用参数? 你必须指定类型吗? Python是强类

What is the best way to check if a variable is a list?

This question already has an answer here: What are the differences between type() and isinstance()? 6 answers These all express different things, so really it depends on exactly what you wish to achieve: isinstance(x, list) check if the type of x is either list or has list as a parent class (lets ignore ABCs for simplicity etc); type(x) is list checks if the type of x is precisely list ;

检查变量是否为列表的最佳方法是什么?

这个问题在这里已经有了答案: type()和isinstance()之间有什么区别? 6个答案 这些都表达了不同的东西,所以真正取决于你想要达到的目标: isinstance(x, list)检查x的类型是list还是list作为父类(为简单起见,忽略ABC); type(x) is list检查x的类型是否精确list ; type(x) == list检查类型是否相同,与元类可能会覆盖__eq__ 所以为了表达以下内容: isinstance(x, list) :是x像list type(x) is list

Difference between isinstance and type in python

This question already has an answer here: What are the differences between type() and isinstance()? 6 answers First check out all the great answers here. type() simply returns the type of an object. Whereas, isinstance(): Returns true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Example: class MyString(str):

python中isinstance和type之间的区别

这个问题在这里已经有了答案: type()和isinstance()之间有什么区别? 6个答案 首先查看这里的所有优秀答案。 type()只是返回一个对象的类型。 而isinstance(): 如果对象参数是classinfo参数的实例或其(直接,间接或虚拟)子类的实例,则返回true。 例: class MyString(str): pass my_str = MyString() if type(my_str) == 'str': print 'I hope this prints' else: print 'cannot check su

Why use isinstance() instead of type()?

This question already has an answer here: What are the differences between type() and isinstance()? 6 answers Let me give you a simple example why they can be different: >>> class A(object): pass >>> class B(A) : pass >>> a = A() >>> b = B() So far, declared a variable A and a variable B derived from A . >>> isinstance(a, A) True >>> t

为什么使用isinstance()而不是type()?

这个问题在这里已经有了答案: type()和isinstance()之间有什么区别? 6个答案 让我给你一个简单的例子,说明它们可能会有所不同: >>> class A(object): pass >>> class B(A) : pass >>> a = A() >>> b = B() 到目前为止,声明了一个变量A和一个从A派生的变量B >>> isinstance(a, A) True >>> type(a) == A True 但 >>> isinstance(b, A) True &g

How to check if a variable is a dictionary in python

This question already has an answer here: What are the differences between type() and isinstance()? 6 answers if type(ele) is dict或者使用isinstance(ele,dict),你可以使用,如果你使用了子类型的字典, d = {'abc':'abc','def':{'ghi':'ghi','jkl':'jkl'}} for ele in d.values(): if isinstance(ele,dict): for k, v in ele.items(): print(k,' ',v)

如何检查变量是否是Python中的字典

这个问题在这里已经有了答案: type()和isinstance()之间有什么区别? 6个答案 if type(ele) is dict或者使用isinstance(ele,dict),你可以使用,如果你使用了子类型的字典, d = {'abc':'abc','def':{'ghi':'ghi','jkl':'jkl'}} for ele in d.values(): if isinstance(ele,dict): for k, v in ele.items(): print(k,' ',v)

Why the super class is empty when define child class?

This question already has an answer here: What is the difference between old style and new style classes in Python? 9 answers In Python 2, not specifying a parent class creates an old-style class. Explicitly inheriting from object creates a new-style class. (There is no difference between class Foo and class Foo() that I know of. Both just result in an old-style class with no parent.) I

定义子类时,为什么超类是空的?

这个问题在这里已经有了答案: Python中的旧风格和新风格类有什么区别? 9个答案 在Python 2中,不指定父类会创建一个旧式类。 显式地从object继承创建一个新风格的类。 (我所知道的class Foo() class Foo和class Foo()之间没有区别,只是导致了一个没有父类的旧式类。) 在Python 3中,所有三种语法都会产生一种新式的类; 没有区别。 看到以下帖子: https://wiki.python.org/moin/NewClassVsClassicClass Py

Class differences in isinstance() in python3 vs python2

This question already has an answer here: What is the difference between old style and new style classes in Python? 9 answers isinstance() and issubclass() return conflicting results 3 answers

python3 vs python2中的isinstance()中的类差异

这个问题在这里已经有了答案: Python中的旧风格和新风格类有什么区别? 9个答案 isinstance()和issubclass()返回冲突的结果3个答案

Python differences in class definitions

This question already has an answer here: Python class inherits object 7 answers What is the difference between old style and new style classes in Python? 9 answers 在Python 3中,所有三者都是相同的,即全部来自object 。

类定义中的Python差异

这个问题在这里已经有了答案: Python类继承对象7的答案 Python中的旧风格和新风格类有什么区别? 9个答案 在Python 3中,所有三者都是相同的,即全部来自object 。