- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumstr.py
More file actions
Latest commit
41 lines (33 loc) · 1.3 KB
/
Copy pathnumstr.py
File metadata and controls
41 lines (33 loc) · 1.3 KB
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
#!/usr/bin/env python
classNumStr(object):
def__init__(self, num=0, string=''):
self.__num=num
self.__string=string
def__str__(self): # define for str()
return'[%d :: %r]'% \
(self.__num, self.__string)
__repr__=__str__
def__add__(self, other): # define for s+o
ifisinstance(other, NumStr):
returnself.__class__(self.__num+ \
other.__num,
self.__string+other.__string)
else:
raiseTypeError, \
'Illegal argument type for built-in operation'
def__mul__(self, num): # define for s*o
ifisinstance(num, int):
returnself.__class__(self.__num*num,
self.__string*num)
else:
raiseTypeError, \
'Illegal argument type for built-in operation'
def__nonzero__(self): # reveal tautology
returnself.__numorlen(self.__string)
def__norm_cval(self, cmpres): # normalize cmp()
returncmp(cmpres, 0)
def__cmp__(self, other): # define for cmp()
returnself.__norm_cval(
cmp(self.__num, other.__num)) + \
self.__norm_cval(
cmp(self.__string, other.__string))