Search code examples
pythonattributes

Is a member variable the same thing as an instance attribute in python?


In the python wiki an attributes were described as variables defined within methods, and in this link: http://pythoncentral.io/introduction-to-python-classes/ they describe val from the code below as a member variable.

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

I am just wondering if this also means that val is an instance attribute (or maybe a class attribute since it was defined in the init section? Sorry if this is a repeated question, but I couldn't find anything confirming this.


Solution

  • Instance/Member variables are values associated with a specific instance of a class. These can be different for each class, and are accessible via classes methods. A class variable is something that is originally the same in each instance of a class. By example, take the following class file:

    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
    

    Note that the instance variables here are prefixed with __ which denotes that these are supposed to be private. Now to use this class:

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

    Hello World

    print object_instance_one.class_variable
    

    Note that this is accessible directly as a class variable and not via a method.

    print object_instance_one.to_string() + object_instance_one.class_variable
    

    Hello World!

    You can override class variables if you wish:

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

    Hello World!!!

    Now because the instance variables are declared as private using __, you would not normally modify these directly but instead use properties to provide methods that allow you to modify these. These properly methods allow you to add setter and getter methods (e.g. validation or type checking). An example:

    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)
    

    Usage:

    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