2016-06-19 6 views
0

python wikiでは、属性はメソッド内で定義された変数とこのリンクに記述されています:http://pythoncentral.io/introduction-to-python-classes/以下のコードからvalをメンバー変数として記述します。メンバー変数は、Pythonのインスタンス属性と同じですか?

class Foo: 
     def __init__(self, val): 
      self.val = val 
     def printVal(self): 
      print(self.val) 

これはまた、それがのinitセクションで定義されたので、valはインスタンス属性(または多分クラス属性であることを意味している場合、これは繰り返し質問であれば私は申し訳ありません?疑問に思って、私は「couldn

+0

インスタンス属性が多く存在し得るあるクラスの特定のインスタンスの属性を指します。クラス属性は、インスタンスによって継承されますが、クラスで定義されている(インスタンスでオーバーライドされていない限り)クラス自体の属性を参照します。 –

答えて

1

インスタンス変数とメンバ変数は、クラスごとに異なる可能性があり、クラスメソッドからアクセスできます。クラス変数は元々同じものですたとえば、クラスの各インスタンスでは、次のクラスファイルを使用します。

class MyClass(object): 
    class_variable = "!" 

    def __init__(self, first_word, second_word): 
     self.__instance_variable_one = first_word 
     self.__instance_variable_two = second_word 

    def to_string(self): 
     return self.__instance_variable_one + " " + self.__instance_variable_two 

ここでは、インスタンス変数に接頭辞__が付いています。これは、これらがプライベートであることを示しています。これは、直接クラス変数としてではなくメソッドを介してアクセス可能であることを

object_instance_one = MyClass("Hello", "World") 
object_instance_one.to_string() 

Hello World

print object_instance_one.class_variable 

!

注:今、このクラスを使用します。

print object_instance_one.to_string() + object_instance_one.class_variable 

Hello World!

あなたが希望する場合、クラス変数を上書きすることができます。

object_instance_one.class_variable = "!!!" 
print object_instance_one.to_string() + object_instance_one.class_variable 

Hello World!!!

をインスタンス変数は__を使用してプライベートとして宣言されているので、あなたは通常、直接ではなく、これらを変更しないだろうプロパティを使用して、これらを変更できるメソッドを提供します。これらの適切なメソッドを使用すると、セッターメソッドとゲッターメソッド(検証や型チェックなど)を追加できます。例:

class MyClass(object): 
class_variable = "!" 

def __init__(self, first_word=None, second_word=None): 
    self.__instance_variable_one = first_word 
    self.__instance_variable_two = second_word 

@property 
def instance_variable_one(self): 
    return self.__instance_variable_one 

@instance_variable_one.setter 
def instance_variable_one(self, value): 
    if isinstance(value, str): 
     self.__instance_variable_one = value 
    else: 
     raise TypeError("instance variable one must be of type str") 

@property 
def instance_variable_two(self): 
    return self.__instance_variable_two 

@instance_variable_two.setter 
def instance_variable_two(self, value): 
    if isinstance(value, str): 
     self.__instance_variable_two = value 
    else: 
     raise TypeError("instance variable two must be of type str") 

def to_string(self): 
    return str(self.__instance_variable_one) + " " + str(self.__instance_variable_two) 

使用:

object_instance_one = MyClass() 
object_instance_one.instance_variable_one = "Hello" 
object_instance_one.instance_variable_two = "World" 
print object_instance_one.to_string() + object_instance_one.class_variable 

Hello World!

object_instance_one.instance_variable_two = 2 

File "C:/MyClass.py", line 38, in
object_instance_one.instance_variable_two = 2 File "C:/MyClass.py", line 28, in > >instance_variable_two raise TypeError("instance variable two must be of type str") TypeError: instance variable two must be of type str

関連する問題