-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice7.py
34 lines (23 loc) · 1.65 KB
/
practice7.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
# Magic Methods
# Python does not, by default, do automatic typecasting when "adding" a string and a number together. For example, print("1" + 2) will raise a TypeError. However, we can override the magic method __add__ in a class to allow for this functionality.
# Implement a class called StrNumeric with the following:
# A constructor method that takes a parameter called value and initializes an instance property called val. We will be typecasting this value as an int later on, so we need to ensure that the typecasting will be possible. Use the built-in function isinstance to check if the value passed in is a str and the built-in str.isnumeric() method to ensure that all characters in the string value are numeric. If the value is a string, but has non-numeric characters, raise an Exception with the message "String value can have only numeric characters.". Otherwise, set the instance property to this value.
# An implementation of the __add__ magic method. This method will take in self (as does every instance method) along with another parameter called thing_2. This method should cast both self.val and thing_2 as ints and return their sum.
# Write your class here.
class StrNumeric:
def __init__(self, value):
if isinstance(value, str) and not value.isnumeric():
raise Exception('String value can have only numeric characters.')
self.val = value
def __add__(self, thing2):
return int(self.val) + int(thing2)
str_1 = StrNumeric("1")
print(str_1 + 2) # 3
str_44 = StrNumeric("44")
print(str_44 + 6) # 50
num_44 = 44
print(num_44 + 6) # 50
try:
not_numeric = StrNumeric("1.2") # Exception: String
except Exception as e:
print(repr(e))