2017-02-03 14 views
0

基本的にはどうすればよいですか?親静的メソッドからのPython呼び出し子静的変数

class Parent: 
    Foo = 'Parent food' 

    @staticmethod 
    def bar(): 
     # want to print whatever the child's Foo is 


class Child(Parent): 
    Foo = 'Child foo' 


# This should print "Child foo" 
Child.bar() 
+4

ためclassmethodを使用することができます。静的メソッドは、その定義によってステートレスです。 –

答えて

2

あなたは_classmethod_、ない_staticmethod_をしたいという

class Parent: 
    Foo = 'Parent food' 

    @classmethod 
    def bar(cls): 
     print cls.Foo 

class Child(Parent): 
    Foo = 'Child foo' 


Child.bar() 
# This will print "Child foo" 
0

コメントに記載されているとおり、必要なものはクラスメソッドです。

class Parent: 
    Foo = 'Parent food' 

    @classmethod 
    def bar(cls): 
     print(cls.Foo) 
関連する問題