简体   繁体   中英

Python Class: Lambda Name Error - Name not defined

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM