python對象判斷
㈠ python 如何區分對象和函數
函數就是一個callable的對象,所有對象只要你實現了它的call方法就跟函數一樣
㈡ Python內部是如何判斷一個對象是True還是False
作者:gao xinge
鏈接:https://www.hu.com/question/53708403/answer/139331035
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
內建函數boolpython中的所有對象都可以用內建函數bool來判斷布爾值是True還是False,如下>>> bool(1)
True
>>> bool(0)
False
>>> bool(True)
True
>>> bool(False)
False
>>> def f(a):
return a
>>> bool(f)
True
>>> bool(__builtins__)
True
>>> import collections
>>> bool(collections)
True
__nonzero__函數和__len__函數內建函數bool的邏輯順序: 如果對象沒有實現__nonzero__函數或者__len__函數,返回True; 如果對象實現了__nonzero__函數,根據__nonzero__函數的返回值判斷; 如果對象沒有實現__nonzero__函數,但實現了__len__函數,根據__len__函數的返回值判斷如下>>> # example one
>>> class f:
def __init__(self, a, b):
self.a = a
self.b = b
>>> t = f(0,1)
>>> bool(t)
True
>>> # example two
>>> class f:
def __init__(self, a, b):
self.a = a
self.b = b
def __nonzero__(self):
return self.a
def __len__(self):
return self.b
>>> t = f(0,1)
>>> bool(t)
False
>>> # example three
>>> class f:
def __init__(self, a, b):
self.a = a
self.b = b
def __len__(self):
return self.b
>>> t = f(1,0)
>>> bool(t)
False
㈢ python里怎麼樣判斷文件名和文件實例對象
python是一個動態類型的語言,這意味著任何一個給出的變數會隨著不同的場合是不同的類型,比如下面的例子中的變數,可以是整數、浮點數、或者字元串類型:
def function(value):
print(value)
function(1)
function(1.0)
function("one")
這時可以使用type()函數來檢查變數的類型,如下面的例子:
# File: builtin-type-example-1.py
def mp(value):
print(type(value), value)
mp(1)
mp(1.0)
mp("one")
輸出結果如下:
==== RESTART: D:/work/csdn/python_Game1/example/builtin-type-example-1.py ====
<class 'int'> 1
<class 'float'> 1.0
<class 'str'> one
>>>
因此可以使用type()函數來判斷一個變數是什麼類型,這里就可以用來判斷變數是字元串類型,還是文件對象,如下:
# File: builtin-type-example-2.py
def load(file):
if isinstance(file, type("")):
file = open(file, "rb")
return file.read()
print(len(load("builtin-type-example-2.py")), "bytes")
print(len(load(open("builtin-type-example-2.py", "rb"))), "bytes")
輸出結果如下:
==== RESTART: D:/work/csdn/python_Game1/example/builtin-type-example-2.py ====
273 bytes
273 bytes
>>>
在這一行代碼:
isinstance(file, type(""))
判斷是否為字元串變數。
㈣ 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中如何判斷一個對象是否一個類的實例
可以用 isinstance(s, myclass)來判斷
如果是s是mycalss的實例,返回True,否則返回False
㈥ python怎麼判斷一個對象的屬性
方法一:通過異常捕捉來實現邏輯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class FooClass:
pass
k = FooClass()
try:
#do some thing you need
print k.att
except AttributeError as e:
#error: has not attribute
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對象是否存在
通過異常捕捉來實現邏輯
class FooClass:
pass
k = FooClass()
try:
#do some thing you need
print k.att
except AttributeError as e:
#error: has not attribute
pass
㈧ 關於python 如何對返回對象進行判斷
例子:
response=requests.get(url,headers=headers)
成功的話會返回一個響應對象:<class 'requests.models.Response'>
㈨ 怎麼判斷 Python 對象是否包含某個屬性
方法一:通過異常捕捉來實現邏輯
class FooClass:passk = FooClass()try: #do some thing you need
print k.attexcept AttributeError as e: #error: has not attribute
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.attelse: #error: has not attribute
pass
另外, 這種問題可以使用google搜索下,善用google
㈩ python判斷變數是不是對象
在python中變數可以理解為是一個對象的引用,既變數不是對象.