Classes
|
Outline: |
super().__init__()
st = super().__repr__()
self.__class__.__name__
- gets a string
representation of the name of runtime class
# v16.py
# a classes example, based on the following Java example:
# http://sandsduchon.org/duchon/Musings/a/scanCodeC.html
# Class Hierarchy:
# CA (string, int)
# +---CB (float, int)
# +---CC (int,
boolean)
# +---CD (int,
boolean, double, string)
# +---CE (float[])
# +---CF (string[])
# +---CG (int[])
import random as rn
import string
def randomword(length):
letters = string.ascii_lowercase
return ''.join(rn.choice(letters) for i in
range(length))
def cleanList (st, r = " "):
return st.replace(", ", r).replace("[",
"").replace("]", "")
class CA:
def __init__ (self):
self.n = randomword (10)
self.id = rn.randrange(12)
# __str__ seems to be seldom a good idea, only used by print
and format
# __repr__ needed to print from or instantiate in a list,
etc., and will be used if __str__ is missing
def __repr__ (self):
return
self.__class__.__name__ + ": " + self.n + " id:" +
str(self.id)
class CB (CA):
# the following would be class=static variables
# da = 2.3 * rn.randrange (200)
# za = rn.randrange (50) + 13
def __init__ (self):
super().__init__()
self.da = 2.3 * rn.randrange
(200)
self.za = rn.randrange (50) +
13
def __repr__ (self):
# the following line creates a NEW instance of CA
# st =
CA().__repr__().replace ("CA", self.__class__.__name__)
# NOTE: in the following the two versions of n and id are the
same
st = super().__repr__()
st += " da:" + str(self.da)
st += " za:" + str(self.za)
return st
class CC (CB):
def __init__ (self):
# DO NOT use CB() instead of super on the following
line
# that will create a new, unreferenced, copy of CB,
# NOT the init the parent of this instance
super().__init__()
self.zb = rn.randrange (22) +
700
self.bb = True
def __repr__ (self):
st = super().__repr__()
st += " zb:" + str(self.zb)
st += " bb:" + str(self.bb)
return st
class CD (CB):
def __init__ (self):
super().__init__()
self.zd = rn.randrange (44) +
1000
self.bd = False
self.dd = rn.randrange (14) *
123.123
self.sd = "String " +
randomword (5)
def __repr__ (self):
st = super().__repr__()
st += " zd:" + str(self.zd)
st += " bd:" + str(self.bd)
st += " dd:" + str(self.dd)
st += " sd:" + str(self.sd)
return st
class CE (CA):
def __init__ (self):
super().__init__()
self.aLe = [1.2, 2.3, 3.4,
44.667, 5.5, 6.86]
def __repr__ (self):
st = super().__repr__()
st += " aLe:" + cleanList(
str(self.aLe))
return st
class CF (CE):
def __init__ (self):
super().__init__()
self.aLf = ["asdfd", "eqwer", "
v c v"]
def __repr__ (self):
st = super().__repr__()
st += " aLf:" + cleanList(
str(self.aLf))
return st
class CG (CE):
def __init__ (self):
super().__init__()
self.zg = [44, 55, 66, 77]
def __repr__ (self):
st = super().__repr__()
st += " zg:" +
cleanList(str(self.zg))
return st
a = []
a += [CA()]
a += [CA()]
a += [CB()]
a += [CB()]
a += [CC()]
a += [CD()]
a += [CE()]
a += [CF()]
a += [CG()]
# n = 5
# a = []
# for i in range (n):
# j = rn.randrange (7)
# if j == 0: a += [CA()]
# if j == 1: a += [CB()]
# if j == 2: a += [CC()]
# if j == 3: a += [CD()]
# if j == 4: a += [CE()]
# if j == 5: a += [CF()]
# if j == 6: a += [CG()]
print (cleanList(str(a), r="\n"))
Output:
CA: scpzcfjwxz id:3
CA: fjgumwkgyg id:0
CB: hxlqfkvogx id:2 da:98.89999999999999 za:60
CB: smxgfsyzjw id:11 da:52.9 za:23
CC: osidflxuux id:8 da:427.79999999999995 za:35 zb:712 bb:True
CD: lnolijgsut id:7 da:416.29999999999995 za:34 zd:1011 bd:False dd:246.246 sd:String hryoo
CE: molpezpsah id:3 aLe:1.2 2.3 3.4 44.667 5.5 6.86
CF: aycaoynrhp id:0 aLe:1.2 2.3 3.4 44.667 5.5 6.86 aLf:'asdfd' 'eqwer' ' v c v'
CG: gmvcmdicpy id:4 aLe:1.2 2.3 3.4 44.667 5.5 6.86 zg:44 55 66 77