この記事では、Python3 の hasattr()、getattr()、setattr()、delattr() 関数をサンプル コードを通して詳しく紹介します。非常に優れており、必要な方は参考にしてください。 hasattr() 関数
hasattr() 関数は、対応する属性が含まれているかどうかを判断するために使用されます 構文:
hasattr(object,name)
パラメータ:
object--オブジェクトhasattr(object,name)
参数:
object--对象
name--字符串,属性名
返回值:
如果对象有该属性返回True,否则返回False
示例:
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=People('aaa') print(hasattr(People,'country')) #返回值:True print('country' in People.__dict__) #返回值:True print(hasattr(obj,'people_info')) #返回值:True print(People.__dict__) ##{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10205d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
getattr()函数
描述:
getattr()函数用于返回一个对象属性值
语法:
getattr(object,name,default)
参数:
object--对象
name--字符串,对象属性
default--默认返回值,如果不提供该参数,在没有对于属性时,将触发AttributeError。
返回值:
返回对象属性值
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=getattr(People,'country') print(obj) #返回值China #obj=getattr(People,'countryaaaaaa') #print(obj) #报错 # File "/getattr()函数.py", line 32, in <module> # obj=getattr(People,'countryaaaaaa') # AttributeError: type object 'People' has no attribute 'countryaaaaaa' obj=getattr(People,'countryaaaaaa',None) print(obj) #返回值None
setattr()函数
描述:
setattr函数,用于设置属性值,该属性必须存在
语法:
setattr(object,name,value)
参数:
object--对象
name--字符串,对象属性
value--属性值
返回值:
无
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=People('aaa') setattr(People,'x',111) #等同于People.x=111 print(People.x) #obj.age=18 setattr(obj,'age',18) print(obj.__dict__) #{'name': 'aaa', 'age': 18} print(People.__dict__) #{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1007d5620>, 'people_info': <function People.people_info at 0x10215d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, 'x': 111}
delattr()函数
描述:
delattr函数用于删除属性
delattr(x,'foobar)相当于del x.foobar
语法:
setattr(object,name)
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) delattr(People,'country') #等同于del People.country print(People.__dict__) {'__module__': '__main__', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10073d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
getattr()関数
getattr() 関数は、オブジェクト属性値を返すために使用されます
構文: getattr(object,name,default)
パラメータ:
object--object name -- 文字列、オブジェクト属性
default -- デフォルトの戻り値 このパラメーターが指定されていない場合、属性がない場合に AttributeError がトリガーされます。
class Foo: def run(self): while True: cmd=input('cmd>>: ').strip() if hasattr(self,cmd): func=getattr(self,cmd) func() def download(self): print('download....') def upload(self): print('upload...') # obj=Foo() # obj.run()🎜🎜🎜🎜🎜🎜setattr() 関数 🎜🎜🎜🎜説明: 🎜🎜setattr 関数、属性値の設定に使用され、属性は存在する必要があります🎜🎜構文:🎜🎜
setattr(object,name,value)
🎜🎜パラメータ: 🎜🎜object -- オブジェクト 🎜🎜name -- 文字列、オブジェクト属性 🎜🎜value -- 属性値 🎜🎜戻り値: 🎜🎜なし🎜🎜🎜🎜rrreee🎜🎜🎜🎜🎜🎜delattr()関数🎜🎜🎜🎜説明: 🎜🎜delattr関数は属性を削除するために使用されます🎜🎜delattr(x,'foobar)はdel xと同等です。ふーばー🎜 🎜文法:🎜🎜setattr(object,name)
🎜🎜パラメータ: 🎜🎜object--object 🎜🎜name--オブジェクトの属性である必要があります🎜🎜戻り値: 🎜🎜なし🎜🎜例: 🎜🎜 🎜🎜rrreee🎜🎜🎜🎜 補足例: 🎜🎜🎜🎜rrreee🎜🎜🎜🎜🎜 関連する推奨事項: 🎜🎜🎜 Python3 ライブラリで numpy 配列属性を表示する方法🎜🎜 🎜🎜🎜以上がPython3 の Hasattr()、getattr()、setattr()、delattr() 関数とサンプル コードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

PythonスクリプトがUNIXシステムで実行できない理由には、次のものが含まれます。1)CHMOD XYOUR_SCRIPT.PYを使用して実行権限を付与する不十分な権限。 2)shebangラインが正しくないか欠落している場合、#!/usr/bin/envpythonを使用する必要があります。 3)環境可変設定が誤っていない場合、OS.Environデバッグを印刷できます。 4)間違ったPythonバージョンを使用して、Shebangラインまたはコマンドラインでバージョンを指定できます。 5)仮想環境を使用して依存関係を分離する依存関係の問題。 6)構文エラー、python-mpy_compileyour_script.pyを使用して検出します。

Pythonアレイの使用は、リストよりも大量の数値データの処理に適しています。 1)配列を保存するメモリを保存します。2)アレイは数値的な値で動作するのが高速です。3)アレイフォースタイプの一貫性、4)アレイはCアレイと互換性がありますが、リストほど柔軟で便利ではありません。

listSareのより良い前提条件とmixdatatypes、whilearraysares優れたスナリカル計算砂の砂を大きくしたデータセット。

numpymanagesmemoryforlargearrayseffictificleusing biews、copies、andmemory-mappedfiles.1)rewsinging withotingcopying、directmodifying theoriginalArray.2)copiescanbecreatedwithcopy()methodforpreservingdata.3)Memory-MapplehandLemassiutasedatasetasedatasetasetasetasetasetasedas

listsinpythondonotrequireimportingamodule、whilearrays fromthearraymoduledoneedanimport.1)listsarebuiltin、versatile、andcanholdmixeddatypes.2)araysaremoremory-efficient-fornumerumerumerumerumerumerdatabutでき、対象となるンドベフェフサメタイプ。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

WebStorm Mac版
便利なJavaScript開発ツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

メモ帳++7.3.1
使いやすく無料のコードエディター

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ホットトピック









