python 判斷是不是同一個對象

python 判斷是不是同一個對象
可以用 isinstance(s, myclass)來判斷
如果是s是mycalss的實例,返回True,否則返回False

Ⅱ Python 判斷一個對象是不是類字元串

雖然type(your_object)==str or type(your_object)==unicode可以有些作用。不過下面的方法也許更好。

is_str=None
try:
is_str=str(your_object)
except:pass

Ⅲ 怎麼判斷 Python 對象是否包含某個屬性

方法一:通過異常捕捉來實現邏輯

classFooClass:

pass


k=FooClass()

try:

#dosomethingyouneed

printk.att

exceptAttributeErrorase:

#error:hasnotattribute

pass

方法二:調用hasattr方法

hasattr(object, name)
說明:判斷對象object是否包含名為name的特性(hasattr是通過調用getattr(ojbect, name)是否拋出異常來實現的)。
參數object:對象。
參數name:特性名稱。

>>> hasattr(list, 'append')

True

>>> hasattr(list, 'add')

False

方法三:使用dir方法

objlist = dir(k)

if 'att' in objlist:

#do some thing you need

print k.att

else:

#error: has not attribute

pass

Ⅳ 怎麼判斷一個變數是不是類 python

python中如何判斷一個變數的數據類型?(原創) 收藏
import types
type(x) is types.IntType # 判斷是否 類型
type(x) is types.StringType #是否string類型
.........
--------------------------------------------------------
超級惡心的模式,不用記住types.StringType
import types
type(x) == types(1) # 判斷是否int 類型
type(x) == type('a') #是否string類型
------------------------------------------------------
使用內嵌函數:
isinstance ( object, classinfo )
Return true if the object argument is an instance of the classinfo argument, or of a (direct or indirect) subclass thereof. Also return true if classinfo is a type object and object is an object of that type. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised. Changed in version 2.2: Support for a tuple of type information was added.
Python可以得到一個對象的類型 ,利用type函數:
>>>lst = [1, 2, 3]
>>>type(lst)
<type 'list'>

不僅如此,還可以利用isinstance函數,來判斷一個對象是否是一個已知的類型。
isinstance說明如下:
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
其第一個參數為對象,第二個為類型名或類型名的一個列表。其返回值為布爾型。若對象的類型與參數二的類型相同則返回True。若參數二為一個元組,則若對象類型與元組中類型名之一相同即返回True。
>>>isinstance(lst, list)
Trueisinstance(lst, (int, str, list))
True
>>>isinstance(lst, (int, str, list))
True

Ⅳ python 判斷對象是否有屬性

hasattr(obj, xxx)

Ⅵ python3 判斷一個對象是不是float

以前有過類似的問題 可以參考版下:權

https://..com/question/389856617.html

Ⅶ python中如何判斷一個對象是某個類型的數組

可以使用 Python Image Library 做,load() 函數會返回一個對象,這個對象我們可回以把它當作一個二維數組對待,而答數組中存放的就是點的 RGB 值,可以很容易地訪問到任何像素點的 RGB 值:
from PIL import Image

# 可以支持很多種圖片格式.
im = Image.open("your_picture.jpg")
pix = im.load()

# 獲得圖片的尺度,可以用於迭代
print im.size

# 獲得某個像素點的 RGB 值,像素點坐標由 [x, y] 指定
print pix[x,y]

# 設置 [x, y] 點的 RGB 的值為 value
pix[x,y] = value

Ⅷ python迭代和如何判斷一個對象是可迭代對象

fromcollectionsimportIterable

ifisinstance(obj,Iterable):
print("Yes")
else:
print("No")

Ⅸ isinstance python 怎麼判斷參數是一個函數對象

>>>importtypes
>>>isinstance(open,types.FunctionType)
False

>>>callable(open)
True

上面的例子可以說明isinstance判斷是不是一個函數對象是不怎麼靠譜的,判斷是不是一個函數對象最好用callable

Ⅹ python如何判斷對象是否為字元串或者其他類型

isinstance(1,int)
True
isinstance('a',str)
True