简体   繁体   English

Python类:Lambda名称错误 - 未定义名称

[英]Python Class: Lambda Name Error - Name not defined

I am a beginner in Python 3.6. 我是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'. 我正在尝试创建一个名为“Week”的类,它将执行某些操作并最终打印变量'avg_mon'的值。

In order to calculate the value of 'avg_mon', I have used the anonymous function 'lambda'. 为了计算'avg_mon'的值,我使用了匿名函数'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. 如上所述,它都被定义为类的一部分,这会导致您的问题: count_mon lambda函数时count_mon不在范围内。

Move this code inside an __init__ function: 将此代码__init__函数中:

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 以下是为什么会发生这种情况的完整解释: 从类定义中的列表解析中访问类变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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