-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_Class_Animal__Scope.py
38 lines (24 loc) · 1.19 KB
/
07_Class_Animal__Scope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Each individual animal gets its own name and age (since they’re all initialized individually), but they all have access to the member variable is_alive, since they’re all members of the Animal class.
class Animal(object):
"""Makes cute animals."""
is_alive = True
def __init__(self, name, age):
self.name = name
self.age = age
zebra = Animal("Jeffrey", 2)
giraffe = Animal("Bruce", 1)
panda = Animal("Chad", 7)
print zebra.name, zebra.age, zebra.is_alive
print giraffe.name, giraffe.age, giraffe.is_alive
print panda.name, panda.age, panda.is_alive
"""
The scope of a variable is the context in which it’s visible to the program.
Not all variables are accessible to all parts of a Python program at all times.
When dealing with classes, you can have:
* variables that are available everywhere (global variables),
* variables that are only available to members of a certain class (member variables), and
* variables that are only available to particular instances of a class (instance variables).
The same goes for functions: some are available everywhere,
some are only available to members of a certain class,
and still others are only available to particular instance objects.
"""