Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 43

Python Exercise Example 43

Python 100 Examples Python 100 Examples

**Question:** Another example of imitating static variables.

**Program Analysis:** Demonstrates how to use Python scope

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

class

Num:

    nNum

=

1

   

def

inc

(

self

)

:

       

self

.

nNum

=

1

       

print

(

'nNum = %d'

%

self

.

nNum

)

if

name

==

'main'

:

    nNum

=

2

    inst

=

Num

(

)

   

for

i

in

range

(

3

)

:

        nNum +

=

1

       

print

(

'The num = %d'

% nNum

)

        inst.

inc

(

)

The output of the above example is:


The num = 3
nNum = 2
The num = 4
nNum = 3
The num = 5
nNum = 4

Python 100 Examples Python 100 Examples