Introduction to Python

davidbpython.com




In-Class Exercise Solutions, Session 13



CLASS VARIABLES AND METHODS

 
Ex. 13.1 Demonstrate class attributes.

In the class below, set a class variable cvar to value 1000. Print the value of cvar in three places: 1) instance a (a.cvar); 2) instance b (b.cvar); 3) the class itself (Something.cvar) Also print the .attr attribute from each of the two instances.

Suggested Solution:
class Something:
    cvar = 1000                    # int, 1000

    def __init__(self, xx):        # self: 'Something' object; xx: int, 1000
        self.attr = xx             # int, 1000

a = Something('hi')                # 'Something' object
b = Something('there')             # 'Something' objects


print(a.cvar)                      # 1000
print(b.cvar)                      # 1000
print(Something.cvar)              # 1000

print(a.attr)                      # hi
print(b.attr)                      # there
 
Ex. 13.2 Create a class method.

Add a class method classincrement(cls) that uses its cls argument to increment the cattr class variable (cattr will be found to be an attribute of cls. Call classincrement() through the instance obj as well as through the class MyClass. The values printed below should both be 1. Before this can work as shown, however, you must decorate classincrement() with @classmethod.

Suggested Solution:
class MyClass:

    cattr = 0                            # int, 0

    @classmethod
    def classincrement(cls):             # cls:  'MyClass' class object
        cls.cattr = cls.cattr + 1        # int, 1

obj = MyClass()                          # 'MyClass' object


obj.classincrement()

print(obj.cattr)                         # 1
print(MyClass.cattr)                     # 1
 
Ex. 13.3 Create a static method.

To the below class add the static method ftoc(temp) which converts a temperature in Fahrenheit to Celcius. The formula is (temp - 32) * 5 / 9 To be a static method, the method must not take self as an argument, and must be decorated with @staticmethod.

Suggested Solution:
class Forecast:

    def __init__(self, forecast, high=0, low=0):   # self: 'Forecast' object; forecast: str; high: int, 62; low:  int, 48
        self.text = forecast                       # str, 'Light rain'
        self.hightemp = high                       # int, 62
        self.lowtemp = low                         # int, 48

    @staticmethod
    def ftoc(temp):                                # temp:  int, 32 (first call below)
        return (temp - 32) * 5 / 9                 # return 0


t = Forecast('Light rain', high=62, low=48)        #

print(t.ftoc(32))                                  # 0
print(t.ftoc(212))                                 # 100
 

LAB 1

 
Ex. 13.4 Set a class variable, 'increment_value', that specifies how much the increment() method should increase the attribute. Add this value to the .counterval attribute in increment().
Suggested Solution:
class Counter:
    increment_value = 3                              # int, 3

    def __init__(self, val):                         # self: 'Counter' object; val: int, 5
        self.counterval = val                        # int, 5

    def increment(self):                             # self: 'Counter' object
        self.counterval = self.counterval + Counter.increment_value       # int, 6


c = Counter(5)                                       # 'Counter' object

c.increment()
c.increment()

print(c.counterval)                                  # 5 + (2 * increment_value)
 
[pr]