How to use a global variable for all class methods in Python?
<ul>
<li><p>I have a <code>Person</code> class, which holds an <code>age</code> property, now I need to make it accessible in all method inside <code>Person</code> class, so that all methods work properly</p></li>
<li><p>My code is as following:</p></li>
</ul>

<pre class="lang-py prettyprint-override"><code>class Person:
    #age = 0
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        if(initialAge &lt; 0):
            print("Age is not valid, setting age to 0.")
            age = 0
        age = initialAge

    def amIOld(self):
        # Do some computations in here and print out the correct statement to the console
        if(age &lt; 13):
            print("You are young.")
        elif(age&gt;=13 and age&lt;18):
            print("You are a teenager.")
        else:
            print("You are old.")

    def yearPasses(self):
        # Increment the age of the person in here
        Person.age += 1 # I am having trouble with this method

t = int(input())
for i in range(0, t):
    age = int(input())         
    p = Person(age)  
    p.amIOld()
    for j in range(0, 3):
        p.yearPasses()       
    p.amIOld()
    print("")
</code></pre>

<ul>
<li><p><code>yearPasses()</code> is supposed to increase the <code>age</code> by 1, but now it doesn't do anything when called</p></li>
<li><p>How do I adapt it to make it work?</p></li>
</ul>

<p>You need <code>age</code> to be an instance attribute of the Person class. To do that, you use the <code>self.age</code> syntax, like this:</p>

<pre><code>class Person:
    def __init__(self, initialAge):
        # Add some more code to run some checks on initialAge
        if initialAge &lt; 0:
            print("Age is not valid, setting age to 0.")
            self.age = 0
        self.age = initialAge

    def amIOld(self):
        # Do some computations in here and print out the correct statement to the console
        if self.age &lt; 13:
            print("You are young.")
        elif 13 &lt;= self.age &lt;= 19:
            print("You are a teenager.")
        else:
            print("You are old.")

    def yearPasses(self):
        # Increment the age of the person in here
        self.age += 1 

#test

age = 12
p = Person(age)  

for j in range(9):
    print(j, p.age)
    p.amIOld()
    p.yearPasses()    
</code></pre>

<p><strong>output</strong></p>

<pre><code>0 12
You are young.
1 13
You are a teenager.
2 14
You are a teenager.
3 15
You are a teenager.
4 16
You are a teenager.
5 17
You are a teenager.
6 18
You are a teenager.
7 19
You are a teenager.
8 20
You are old.
</code></pre>

<hr>

<p>Your original code had statements like </p>

<pre><code>age = initialAge 
</code></pre>

<p>in its methods. That just creates a local object named <code>age</code> in the method. Such objects don't exist outside the method, and are cleaned up when the method terminates, so the next time you call the method its old value of <code>age</code> has been lost. </p>

<p><code>self.age</code> is an attribute of the class instance. Any method of the class can access and modify that attribute using the <code>self.age</code> syntax, and each instance of the class has its own attributes, so when you create multiple instances of the Person class each one will have its own <code>.age</code>. </p>

<p>It's also possible to create objects which are attributes of the class itself. That allows all instances of the class to share a single object. Eg, </p>

<pre><code>Person.count = 0
</code></pre>

<p>creates a class attribute named <code>.count</code> of the Person class. You can also create a class attribute by putting an assignment statement outside a method. Eg,</p>

<pre><code>class Person:
    count = 0
    def __init__(self, initialAge):
        Person.count += 1
        # Add some more code to run some checks on initialAge
        #Etc
</code></pre>

<p>would keep track of how many Person instances your program has created so far.</p>

python class object methods instance