Masoud Rahimi
Masoud Rahimi

Reputation: 6031

How to access a class variable shadowed by an instance variable?

I couldn't find a specific answer to my question. As you might know, a class in Python can have both class variables and instance variables. If a class has a class variable and an instance variable that both have the same name, how can I access both in another method of the class?

Suppose the following code. How can I access both variables myvar?

class myclass:
    myvar = "class variable"

    def __init__(self):
        self.myvar = "instance variable"
        pass

    def test(self):
        # difference between self.myvar and myclass.myvar
        return self.myvar


class_instance = myclass()
print(class_instance.test())
print(class_instance.myvar)     # "class variable" or "instance variable"

Here if I remove myvar from __init__ the self.myvar would refer to the class variable and if I add it, self.myvar would refer to the instance variable. So is it a rule that instance variables have precedence over class variable? I know that I can use myclass.myvar for referring to the class member, but in the last line of the above code class_instance.myvar would refer to both class variable and instance variable.

Upvotes: 1

Views: 766

Answers (5)

Mate I've seen another way of finding this, and when I've seen that, you have come to my mind and I wanted to share it with you, because, I thought like, you might be searching for this solution:

With code that've written below you can reach both same named variables with underscoring first one.

   class myclass:
    __myvar = "class variable"
    def __init__(self):
        self.myvar = "instance variable"
        pass
    def test(self):
        # difference between self.myvar and myclass.myvar
        return self.myvar


class_instance = myclass() 
print(class_instance.test())
print(class_instance._myclass__myvar)

Upvotes: 0

tsh
tsh

Reputation: 2375

First of all, I suppose you are using Python 3. If that was not the case, you should change the first line to read: class myclass(object)

To access the instance variable, you can use: class_instance.myvar

To access the class variable, you can use: type(class_instance).myvar.

Upvotes: 1

Sharad Venkataraman
Sharad Venkataraman

Reputation: 114

When you are instantiating the class,

some_class = myclass()

you are basically overwriting the myvar value (when the __init__ constructor is called)

You can observe by using this code -

class myclass:
    myvar = "class variable"
    def __init__(self):
        print(self.myvar) #added a print statement
        self.myvar = "instance variable"
        pass
    def test(self):
        # difference between self.myvar and myclass.myvar
        return self.myvar


class_instance = myclass() 
print(class_instance.test())
print(class_instance.myvar)

Output is -

class variable #due to print command
instance variable #command called after the overwriting of myvar variable
instance variable #command called after the overwriting of myvar variable

Upvotes: 0

Evan Benn
Evan Benn

Reputation: 1699

All you need to do to get the class variable is access the class:

print(some_class.test2())
print(myclass.myvar)

Or, if you dont have the class in scope:

print(some_class.test2())
print(type(someclass).myvar)

Upvotes: 1

class myclass:
    myvar = "class variable"

    def __init__(self):
        self.myvar = "instance variable"
        pass
    def test2(self):
        return myclass.myvar


some_class = myclass()
print(some_class.test2())
print(some_class.myvar)

This might help you! We are finding both of them :)

Upvotes: -1

Related Questions