Python Forum

Full Version: NameError
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting a NameError that I do not understand.
It appears the function "combo" is defined in the proper place, so "combo not found" is puzzling to me.

class Solution(object):
    
    def combo(self, l1, l2):
        cs = []
        for i in range(len(l1)):
            for j in range(len(l2)):
                cs.append(l1[i] + l2[j])
        return cs
    
    
    def letterCombinations(self, digits):
        s = combo(["a", "b", "c"], ["d", "e", "f"])
        return s
Error:
NameError: global name 'combo' is not defined s = combo(["a", "b", "c"], ["d", "e", "f"]) Line 16 in letterCombinations (Solution.py) ret = Solution().letterCombinations(param_1) Line 39 in _driver (Solution.py) _driver() Line 49 in <module> (Solution.py)
Instead of combo(), call self.combo(). Also you can just write class Solution: It is implicit that object is a parent class.
(Oct-18-2022, 01:45 PM)Gribouillis Wrote: [ -> ]Instead of combo(), call self.combo(). Also you can just write class Solution: It is implicit that object is a parent class.

Thank you Griboullis. Adding "self" worked, but puzzling why I get this NameError unless there is another "combo" in python, and I was not able to find it.
There is no combo function. Class Solution has an attribute named combo, so you can call Solution.combo, or you can use an instance of Solution to call combo.