【问题标题】:Python: Unable to call function within a seperate function? (undefined name 'getItemClassiness')Python: Unable to call function within a seperate function? (undefined name \'getItemClassiness\')
【发布时间】:2022-12-02 05:30:18
【问题描述】:

For some reason the getClassiness Function does not work as it is not able to call the helper function getItemClassiness. Is there any reason this might be? Thanks!

class Classy(object):
    def __init__(self):
        self.items = []
    
    def addItem(self, item):
        self.items.append(item)
        
    def getItemClassiness(item):
        if item == "tophat":
            return 2
        if item == "bowtie":
            return 4
        if item == "monocle":
            return 5
        return 0
    
    
    def getClassiness(self):
        total = 0
        for item in self.items:
            x = getItemClassiness(item)
            total += x
        return total

# Test cases

me = Classy()

# Should be 0
print(me.getClassiness())


# Should be 2
me.addItem("tophat")
print(me.getClassiness())

me.addItem("bowtie")
me.addItem("jacket")
me.addItem("monocle")
print(me.getClassiness())
# Should be 11


me.addItem("bowtie\n")
print(me.getClassiness())
# Should be 15

You can use this class to represent how classy someone or something is. "Classy" is interchangable with "fancy". If you add fancy-looking items, you will increase your "classiness". Create a function in "Classy" that takes a string as input and adds it to the "items" list. Another method should calculate the "classiness" value based on the items. The following items have classiness points associated with them: "tophat" = 2 "bowtie" = 4 "monocle" = 5 Everything else has 0 points. Use the test cases below to guide you!

【问题讨论】:

  • Sorry, I misread. There are two problems here: getItemClassiness should be a @staticmethod, and it needs to be explicitly looked up like Classy.getItemClassiness - yes, even within other Classy methods. Python does not have "implicit this" - hence all the explicit self parameters - so other methods of the class are not in the local scope.

标签: python function


【解决方案1】:

You should declare getItemClassiness as a static method because it doesn't require a specific instance. Then you can call the function as you would an instance method.

    @staticmethod
    def getItemClassiness(item):
        ...
    
    
    def getClassiness(self):
        ...
        for item in self.items:
            x = self.getItemClassiness(item)

But still it won't give you 15 for the last test case, because "bowtie" != "bowtie ". If you intend to ignore white space at the start or the end of the string, use str.strip().

【讨论】:

  • Works! Thank you so much. I am used to Java and just started with python, didn't know about the @staticmethod. I appreciate ur help!
【解决方案2】:

In line 21 call for a class method is made without using the self keyword.

 x = self.getItemClassiness(item)

Similarly on line 8 in self keyword is required with as parameter for function definition of getItemClassiness

def getItemClassiness(self, item):

【讨论】:

  • Thanks! I tried that before but kept getting this error, Im not sure what is wrong? x = self.getItemClassiness(item) TypeError: Classy.getItemClassiness() takes 1 positional argument but 2 were given
  • you can also create the function outside the class and use it without referencing to object.
  • Ok, so the x = self.getItemClassiness(item) is correct, however I just needed to add an @staticmethod above the getItemClassiness() method, and now it works! Thank you for your help!
  • @SDC123 It helps to think of self.getItemClassiness(item) as Classy.getItemClassiness(self, item).
  • Ok, that makes sense. I guess I was just kinda confused as self is not in java. I appreciate the help!
【解决方案3】:

Here is what I did using Static Method. Got the right output in Test Cases.

class Classy(object):
def __init__(self):
    self.items = []
    
def addItem(self, item):
    self.items.append(item)
    
@staticmethod
def getItemClassiness(item):
    if item == "tophat":
        return 2
    if item == "bowtie":
        return 4
    if item == "monocle":
        return 5
    return 0


def getClassiness(self):
    total = 0
    for item in self.items:
        x = self.getItemClassiness(item)
        total += x
    return total

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2022-12-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多