検索
ホームページバックエンド開発Python チュートリアルPython クラスの属性とは何ですか? Python でプロパティにアクセスするにはどうすればよいですか?

Python クラス属性 は、言語に付属する関数です。この記事では、サンプル分析を通じて、Python クラス関数とは何か、および Python を使用してアクセスする方法について説明します。属性 。

#1.Python クラスの属性は:

1.__dict__: クラスの属性 (クラスのデータ属性で構成される辞書が含まれます)

2.__doc__: クラスのドキュメント文字列

3.__name__:クラス名

4.__module__: クラス定義が配置されているモジュール (クラスの完全名は「__main__.className」です。クラスがインポートされたモジュール mymod に配置されている場合、className.__module__ はmymod に等しい)

5.__bases__: クラスのすべての親クラス要素 (すべての親クラスで構成されるタプルが含まれます)

2.それでは、どうやっていくかというと、Python を使用して属性にアクセスするのはどうでしょうか。 ピリオド . を使用して、オブジェクトのプロパティにアクセスできます。クラス変数にアクセスするには、次のクラス名を使用します。

emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

連絡先 1 と 2、例を挙げてみましょう

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 class Employee:   '所有员工的基类'
   empCount = 0
 
   def __init__(self, name, salary):      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):   
     print "Total Employee %d" % Employee.empCount
 
   def displayEmployee(self):      
     print "Name : ", self.name,  ", Salary: ", self.salary
 print "Employee.__doc__:", Employee.__doc__
 print "Employee.__name__:", Employee.__name__
 print "Employee.__module__:", Employee.__module__
 print "Employee.__bases__:", Employee.__bases__
 print "Employee.__dict__:", Employee.__dict__

上記のコードを実行した出力結果は次のとおりです:

Employee.__doc__: 所有员工的基类
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount': 
<function displayCount at 0x10a939c80>, &#39;empCount&#39;: 0, &#39;displayEmployee&#39;: 
<function displayEmployee at 0x10a93caa0>, &#39;__doc__&#39;: 
&#39;\xe6\x89\x80\xe6\x9c\x89\xe5\x91\x98\xe5\xb7\xa5\xe7\x9a\x84\xe5\x9f\xba\xe7\xb1\xbb&#39;, 
&#39;__init__&#39;: <function __init__ at 0x10a939578>}

#

以上がPython クラスの属性とは何ですか? Python でプロパティにアクセスするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python:コンパイラまたはインタープリター?Python:コンパイラまたはインタープリター?May 13, 2025 am 12:10 AM

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

ループvs whileループ用のpython:いつ使用するか?ループvs whileループ用のpython:いつ使用するか?May 13, 2025 am 12:07 AM

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

Pythonループ:最も一般的なエラーPythonループ:最も一般的なエラーMay 13, 2025 am 12:07 AM

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

ループの場合、およびPythonのループ:それぞれの利点は何ですか?ループの場合、およびPythonのループ:それぞれの利点は何ですか?May 13, 2025 am 12:01 AM

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

Python:編集と解釈に深く掘り下げますPython:編集と解釈に深く掘り下げますMay 12, 2025 am 12:14 AM

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?May 12, 2025 am 12:09 AM

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

ループ対pythonのループの場合:説明されたキーの違いループ対pythonのループの場合:説明されたキーの違いMay 12, 2025 am 12:08 AM

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

ループのために:実用的なガイドループのために:実用的なガイドMay 12, 2025 am 12:07 AM

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

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

ホットツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境