4

I am a beginner in Python 3.6. I'm trying to create a class called 'Week' which will perform certain operations & ultimately print the value of the variable 'avg_mon'.

In order to calculate the value of 'avg_mon', I have used the anonymous function 'lambda'.

from datetime import datetime
from datetime import date
from operator import add
from operator import itemgetter

class Week():
    blank_mon=[0]*24
    sum_mon=blank_mon
    count_mon=0

    print ("Blank Monday: ",blank_mon)
    #curr_mon=[1,0,2,0,3,0,4,0,5,0,3,0,3,0,2,0,1,0,2,0,1,0,1,0]
    curr_mon=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
    print ("Current Monday",curr_mon)
    count_mon = count_mon + 1
    print ("Monday Count:",count_mon)
    sum_mon=list(map(add,sum_mon,curr_mon))                   #Adds all the Mondays together for each hour
    print ("Total sum of all Mondays::",sum_mon)
    avg_mon = list(map(lambda w_mon: float(w_mon)/count_mon,sum_mon))   #Gets the average of the Mondays for each hour
    print ("Average Monday::",avg_mon)


mon = Week()

When I execute the code, I get the following error. I do not see any spelling/naming errors in my code. I can't figure out the reason behind such an error.

Blank Monday:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Current Monday [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Monday Count: 1
Total sum of all Mondays:: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Traceback (most recent call last):
File "C:\Users\Admin\Documents\GitHub\Doze\functest.py", line 27, in <module>
class Week():
File "C:\Users\Admin\Documents\GitHub\Doze\functest.py", line 40, in Week
avg_mon = list(map(lambda w_mon: float(w_mon)/count_mon,sum_mon))   #Gets the average of the Mondays for each hour
File "C:\Users\Admin\Documents\GitHub\Doze\functest.py", line 40, in <lambda>
avg_mon = list(map(lambda w_mon: float(w_mon)/count_mon,sum_mon))   #Gets the average of the Mondays for each hour
NameError: name 'count_mon' is not defined

1 Answer 1

4

You probably want to put some of this code into a constructor. As written, it's all defined as part of the class, which is causing your problem: count_mon isn't in scope when the lambda function is called.

Move this code inside an __init__ function:

class Week(): 
    def __init__(self):
        blank_mon=[0]*24
        sum_mon=blank_mon
        count_mon=0

        print ("Blank Monday: ",blank_mon)
        #curr_mon=[1,0,2,0,3,0,4,0,5,0,3,0,3,0,2,0,1,0,2,0,1,0,1,0]
        curr_mon=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
        print ("Current Monday",curr_mon)
        count_mon = count_mon + 1
        print ("Monday Count:",count_mon)
        sum_mon=list(map(add,sum_mon,curr_mon))                   #Adds all the Mondays together for each hour
        print ("Total sum of all Mondays::",sum_mon)
        avg_mon = list(map(lambda w_mon: float(w_mon)/count_mon,sum_mon))   #Gets the average of the Mondays for each hour
        print ("Average Monday::",avg_mon)

Here's the full explanation for why this happens: Accessing class variables from a list comprehension in the class definition

Sign up to request clarification or add additional context in comments.

3 Comments

That worked. Thanks. But could you explain the problem.
To be honest, I'm not entirely sure. It seems to be scope related: when your lambda function is called, count_mon isn't in scope.
Ok, here's an explanation: stackoverflow.com/questions/13905741/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.