-
Notifications
You must be signed in to change notification settings - Fork 1
/
class2.py
executable file
·137 lines (103 loc) · 3.39 KB
/
class2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# Module: class2.py
# Purpose: class test #2
# Date: N/A
# Notes:
# 1) Test python code
# https://docs.python.org/2/tutorial/classes.html
# simple class deriving from dict, showing doc and class variables
# - note that there is no data hiding/protection/private variables
# - note you can add to the class data attributes outside the class
# - by convention, a variable starting with _ should be treated as private
class xx1(dict):
# define class variables shared by all instances
"""xx1 test class"""
junk="xx1 junk"
_p="PSEUDO-PRIVATE"
# constructor: define name of the class
def __init__(self, name, x=0, y=0):
self.name = name
# self.x = x
# self.y = y
self.l = [] # sample list
self.__update(x, y, None)
def add_l(self, item):
self.l.append(item)
# xprint: create string and return it with lots of class data
def xprint(self):
return("XX1 %s %s %s" % (self.name, self.junk,self.__doc__))
def sprint(self):
return("%s %d %d %s" % (self.name, int(self.x), int(self.y), self.iprint()))
def iprint(self):
return(str(self.l))
def getp(self):
return(self._p)
def update(self, x, y, item):
self.add_l(item)
self.x = x
self.y = y
__update = update # private copy, name mangled
# define function outside the class, N+1 args, first is "self"
# - must define before defining in the class
def xp(k):
return ('xp: nothing')
# very simple class demonstrating that "self" is custom, not a language feature
# -- in the below, the "self" is replaced by "k"
# -- use "self" in classes not like below as this is non-standard!
class j:
f = xp # define f as the function xp
def __init__(k,jj=0):
k.jj=jj
# main code
print "class2.py: test of Python classes #2"
print "Test of simple class demonstrationg no use of \"self\""
jx=j(12)
print "jx.jj=", jx.jj
print "jx.f=", jx.f()
print "type of jj is ", jx.jj.__class__
print "Test simple class with list, print automatic dict in the class"
x1=xx1("instance1") # create an instance
x1.add_l('aa')
x1.add_l('bb')
x1.add_l(3.14)
print "Result=", x1.xprint()
print "Dictionary=", x1.__dict__
print "List=", x1.iprint()
print "Adding pi to x1 outside of x1"
x1.pi=3.14
print "Dictionary=", x1.__dict__
print "Pseudo-private _p: ", x1.getp()
x1.update(22, 33, "ITEM")
print "Updating class: ", x1.sprint()
print "Null class used as struct"
class Employee:
pass
john = Employee() # Create an empty employee record
# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000
print "values=", john.name, john.dept, john.salary
print "Dictionary=", john.__dict__
# same as john.dept:
print "...and dept=", john.__dict__['dept']
# simple class with an iterator, from examples
class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def next(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
print "Create test object with iterator"
rev="This is a test"
ir=iter(rev)
print "Using Iter: ", ir.next()
print "Use in a FOR loop"
for char in rev:
print "C=", char