Python class change instance variable

Have you thought about using better data structures? Like dictionaries i.e.

class Board[object]:
    def __init__[self]:
        self.dict = {"one":"1", "two":"2", "three":"3"}

And then you could do something like:

>>> a = Board[]
>>> a.dict
{'three': '3', 'two': '2', 'one': '1'}
>>> for element in a.dict:
    a.dict[element] = element+"x"
>>> a.dict
{'three': 'threex', 'two': 'twox', 'one': 'onex'}
>>> a.dict["one"] = "1"
>>> a.dict
{'three': 'threex', 'two': 'twox', 'one': '1'}

The solution you're looking for is also possible [most likely with some very very weird getattrs etc... and I wouldn't really recommend it.

Edit1 It turns out [after checking] that your class attributes will be stored in a object.__dict__ anyhow. SO why not use your own.

Just also to clarify it is possible emulating container objects with your own class by defining __getitem__ and __setitem__ methods like bellow:

class Board[object]:
    def __init__[self]:
        self.dict = {"one":"1", "two":"2", "three":"3"}
    def __getitem__[self,key]:
        return self.dict[key]
    def __setitem__[self, key, value]:
        self.dict[key] = value

Which means you don't have to keep writing a.dict everywhere and can pretend your class is the container [dict] like bellow:

>>> a = Board[]
>>> a.dict
{'three': '3', 'two': '2', 'one': '1'}
>>> a["one"]
'1'
>>> a["one"] = "x"
>>> a.dict
{'three': '3', 'two': '2', 'one': 'x'}

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In the previous fact, we have seen that Python doesn’t have the static keyword. All variables that are assigned a value in the class declaration are class variables.

    We should be careful when changing the value of a class variable. If we try to change a class variable using an object, a new instance [or non-static] variable for that particular object is created and this variable shadows the class variables. Below is a Python program to demonstrate the same.

    Python3

    class CSStudent:

        stream = 'cse'    

        def __init__[self, name, roll]:

            self.name = name

            self.roll = roll

    a = CSStudent["Geek", 1]

    b = CSStudent["Nerd", 2]

    print ["Initially"]

    print ["a.stream =", a.stream ]

    print ["b.stream =", b.stream ]

    a.stream = "ece"

    print ["\nAfter changing a.stream"]

    print ["a.stream =", a.stream ]

    print ["b.stream =", b.stream ]

    Output: 

    Initially
    a.stream = cse
    b.stream = cse
    
    After changing a.stream
    a.stream = ece
    b.stream = cse

    We should change class variables using class names only. 

    Python3

    class CSStudent:

        stream = 'cse'    

        def __init__[self, name, roll]:

            self.name = name

            self.roll = roll

    a = CSStudent["check", 3]

    print "a.stream =", a.stream

    CSStudent.stream = "mec"

    print "\nClass variable changes to mec"

    b = CSStudent["carter", 4]

    print "\nValue of variable steam for each object"

    print "a.stream =", a.stream

    print "b.stream =", b.stream

    Output: 

    a.stream = cse
    
    Class variable changes to mec
    
    Value of variable steam for each object
    a.stream = mec
    b.stream = mec

    This article is contributed by Nikhil Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
     


    Can you change instance variables Python?

    We can modify the value of the instance variable and assign a new value to it using the object reference. Note: When you change the instance variable's values of one object, the changes will not be reflected in the remaining objects because every object maintains a separate copy of the instance variable.

    Can instance variables be changed?

    The value of an instance variable can be changed by any method in the class, but it is not accessible from outside the class.

    How do I change a class attribute in Python?

    But be careful, if you want to change a class attribute, you have to do it with the notation ClassName. AttributeName. Otherwise, you will create a new instance variable.

    Can you change a global variable in a class Python?

    You can access the global variables from anywhere in the program. However, you can only access the local variables from the function. Additionally, if you need to change a global variable from a function, you need to declare that the variable is global. You can do this using the "global" keyword.

    Chủ Đề