Using functools.partial in the class structure, "name" I "is not defined"

Below is a greatly simplified version of my code. After __init__()there are several functions.

I am trying to use functools.partialto create different versions of a base comparisonfunction that refers to a function created earlier in the class calculation. One version of this comparison function may be grade_comparisonas shown below.

class Analysis(mybaseclass):

    def __init__(self, year, cycle):
....

    def calculation(self, subject):
        print subject

    def comparison(subject, **kwargs):
        self.calculation(subject)

    grade_comparison = functools.partial(comparison, infoList1 = ['A', 'B'])

When I run my code, the error appears NameError: global name 'self' is not defined. I tried adding selfto many combinations that seemed logical - one example below.

self.grade_comparison = functools.partial(comparison, self, infoList1 = ['A', 'B'])

This change led to this error, NameError: name 'self' is not defined When I add myself to the comparison function (see below):

def comparison(self, subject, **kwargs):
        self.calculation(subject)

, TypeError: comparison() takes at least 2 arguments (1 given). , , ! , .

+4
1

, , , partial:

class Analysis(object):

    def calculation(self, subject):
        print subject

    def comparison(self, subject, **kwargs):
        self.calculation(subject)

    def grade_comparison(self, subject):
        return self.comparison(subject, infoList1=['A', 'B'])
+3

Source: https://habr.com/ru/post/1623481/


All Articles