Introduction to Python
davidbpython.com
In-Class Exercises, Session 12
|
CLASSES - INSTANCES AND METHODS |
|
| Ex. 12.1 | Define a class. |
|
Use the class statement with name MyClass (or you may substitute a name of your own choice). To fill the otherwise empty block, use the pass statement. Initialize an instance of the class and print its type to show that it is an instance of the class. |
|
Expected Output:
<class '__main__.MyClass'> |
|
| Ex. 12.2 | Create an 'instance' method. |
|
Add to the class Hola below a new method, saludar(), that prints "bienvenidos a todos" (or whatever greeting you'd prefer) when it is called. |
|
class Hola:
""" una clase que is amigable """
# your code here
yo = Hola()
yo.saludar() # should print the greeting
|
|
Expected Output:
bienvenidos a todos |
|
| Ex. 12.3 | Add argument and return value to method. |
|
Add the method doubleit() below, that doubles the value passed to it. |
|
class Numbers:
""" various number methods """
num = Numbers()
val = num.doubleit(5)
print(val) # 10
val2 = num.doubleit(100)
print(val2) # 200
|
|
Expected Output:
10 200 |
|
|
LAB 1 |
|
| Ex. 12.4 | Create a class Time that has a method get_time() that returns the current time. Call as shown. |
|
(A string showing the current time can be obtained from the time module using time.ctime().) |
|
import time
# your code here
obj = Time()
print(obj.get_time()) # should show the current time
|
|
Expected Output:
Sat Oct 17 18:41:34 2020 (as your specific date and time) |
|
| Ex. 12.5 | Create a class Random that has a method get_rand(val) that returns a random number from 1 to the specified value. Call as shown. |
|
(A random number can be generated with random.randint(x, y) where x and y are the minimum and maximum values.) |
|
import random
# your code here
obj = Random()
val = obj.get_rand(5) # random number from 1-5
print(val)
val2 = obj.get_rand(18) # random number from 1-18
print(val2)
|
|
Expected Output:
4 3 |
|
| Ex. 12.6 | Create a class Math with method add() that takes two integer arguments and returns the values summed. |
# your code here
obj = Math()
mysum = obj.add(5, 10) # 15
print(mysum)
mysum2 = obj.add(100, 150) # 250
print(mysum2)
|
|
Expected Output:
15 250 |
|
|
CLASSES - CONSTRUCTOR, CLASS ATTRIBUTES AND METHODS |
|
| Ex. 12.7 | Demonstration: note the unique identifier of self and an instance from a class. |
|
At the bottom of the code below, print obj, then call obj.something(), noting that this method prints self. Compare the hex codes that identify the instance. Use a print() statement to create a blank line. Next, print obj2 and call obj2.something(), and note the output, particularly the hex codes. |
|
class Do:
def something(self):
print(self)
obj = Do()
obj2 = Do()
# your code here
|
|
Expected Output:
<__main__.Do object at 0x10d648350> <__main__.Do object at 0x10d648350> <__main__.Do object at 0x10d648390> <__main__.Do object at 0x10d648390> |
|
| Ex. 12.8 | Create an __init__() method. |
|
Add a method to the below class, __init__(self) that inside the function announces and prints the argument self, i.e. print(f'self: {self}'). Construct 2 new instances, and then print each instance. Put a blank line between each instance. |
|
class Be:
""" this class is something! """
obj1 = Be()
print(f'object: {obj1}')
print()
obj2 = Be()
print(f'object: {obj2})
|
|
Expected Output:
object: <__main__.Be object at 0x10d648250> self: <__main__.Be object at 0x10d648250> object: <__main__.Be object at 0x10d648a50> self: <__main__.Be object at 0x10d648a50> |
|
|
(Note that the above 0x hex codes will not be the same as mine, but the values in each pair should match.) |
|
| Ex. 12.9 | Set an instance attribute in __init__(). |
|
Create a method __init__(self, num) that sets numin self as a .value attribute. At bottom, print obj.value to see the value. |
|
class Live:
""" a class that just wants to live """
# your __init__() code here
obj = Live(5)
# print obj.value here
|
|
Expected Output:
5 |
|
| Ex. 12.10 | Create a "getter" method. |
|
Create a method get_value() that returns the .value attribute from the instance. |
|
class Say:
def __init__(self, val):
self.value = val
# your code here
obj = Say(100)
vl = obj.get_value()
print(vl) # 100
|
|
Expected Output:
100 |
|
|
LAB 2 |
|
| Ex. 12.11 | Create class Counter, initialized with an integer as shown below. Set the attribute .counterval in __init__ so that it is available in the instance. |
# your code here
c = Counter(5)
print(c.counterval) # 5
|
|
Expected Output:
5 |
|
| Ex. 12.12 | Update Counter with an .increment() method that increments the attribute value each time it is called. Call .increment() twice, then print the value of .counterval through the instance. |
class Counter:
def __init__(self, val):
self.counterval = val
# your code here
c = Counter(5)
c.increment()
c.increment()
print(c.counterval) # 7
|
|
Expected Output:
7 |
|
| Ex. 12.13 | Update Counter with a show_value() method that returns the attribute value. |
class Counter:
def __init__(self, val):
self.counterval = val
def increment(self):
self.counterval = self.counterval + 1
# your code here
c = Counter(5)
c.increment()
c.increment()
print(c.counterval) # 7
|
|
Expected Output:
7 |
|