簡體   English   中英

python類的方法

[英]python classes methods of methods

由於我剛接觸python,所以我可能會問一個明顯或愚蠢的問題,但我真的很想學習這個概念。

class Classname:
     def method_1(self):
        ........
      ...................



instance = Classname()

instance.method_1.method_of_1.method_n

我的問題是這種語法是否暗示method_1method_of_1method_n確實是Classname方法或屬性。 如果它們是方法,則不應將它們作為instance.method_1().method_of_1().method_n() 我問這個的原因是因為這種語法經常在Matplotlib中見到,例如Matxlotlib,那里有ax.yaxis.set_ticks() 這是否最后一個例子說,我們正在訪問的屬性ax稱為yaxis ,然后調用一個方法set_ticks() 因此語法暗示: classname.attribute.method 您如何建立這樣的層次結構? 你能指導我去寫作的地方讀書嗎? 我會自己做,但不知道我到底在看什么。 我讀過有關類的信息,但沒有看到帶有兩個或多個“點”的任何東西,例如classname.attribute.method

不熟悉Matplotlib ,但是有可能有類似的子類:

class Foo():
    a = 1
    class Bar():
        b = 2
        def hello():
             print('world')

您可以在其中調用Foo.Bar.hello()並顯示“ world”。

這可以無限進行。 由於Python是基於對象的,因此不一定遵循固定的結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM