欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何獲取對象大小和文件大小

 更新時間:2024年01月20日 09:12:14   作者:Robin_Pi  
這篇文章主要介紹了Python如何獲取對象大小和文件大小問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Python獲取對象大小和文件大小

  • sys.getsizeof:計算Python對象的大?。ㄗ止?jié))
  • os.path.getsize():獲得文件的大?。ㄗ止?jié))
import os, sys

# sys.getsizeof()
print(sys.getsizeof('1'))

print(sys.getsizeof([]))

print(sys.getsizeof(1))

print(sys.getsizeof(dict()))

print(sys.getsizeof(set()))

print()

# os.pat.getsize()

print(os.path.getsize('write_file_a+.txt'))

50
64
28
240
224

20

python定義對象的比較方法

有時候我們需要比較兩個對象。比如哪個對象大,哪個對象小。

如果我們不告訴python如何比較,那么Python是不知道如何進行比較的。

下面提供實例:

#__eq__(self,other):
#在使用==比較運算符比較兩個對象是否相等的時候會調(diào)用這個方法。
#如果是相等,那么應(yīng)該返回True,否則返回False。

#__ne__(self,other):
#在使用!=比較運算符來比較兩個對象是否不相等的時候會調(diào)用這個方法。
#如果這兩個對象不想等,那么應(yīng)該返回True,否則返回False。

#__lt__(self,other):
#在使用<比較運算符來比比較兩個對象大小的時候會調(diào)用這個方法。
#如果self<other,那么應(yīng)該返回True,否則返回False。

#<=和>=的解決方案:
#以上講了<、>、==以及!=的執(zhí)行方法。
#沒有講<=和>=這兩個運算符執(zhí)行的方法,其實這兩個方法是這樣執(zhí)行的,
#拿<=為例來講,首先執(zhí)行<判斷,如果為False,那么會再執(zhí)行==判斷,
#如果都為False,那么就返回False。
class class(object):

	def __init__(self,param1,param2):
		self.param1= 
		self.param2 = param2

	def __eq__(self,other):
		if self.param1==other.param1 and self.param2==other.param2:
			return True
		else:
			return False

	def __ne__(self,other):
		if self.param1!=other.param1 or self.param2!=other.param2:
			return True
		else:
			return False

	def __lt__(self,other):
		if self.param1 < other.param1:
			return True
		else:
			if self.param1==other.param1:
				return True if self.param2<other.param2 else False
			else:
				return False

	def __gt__(self,other):
		if self.param1>other.param1:
			return True

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論