0

I have two classes for in case:

testing.py :

class Functions:
    def mapping(func, x):
        return func(x)

and I try to run the code below:

import testing

def doubleMe(data):
    return data * data

res = stream.Functions.map(testdouble, [1,2,3,4,5])
print res

After I try to run the code, I got the error

TypeError: unbound method mapping() must be called with Functions instance as first argument (got function instance instead)

I not too sure what what have went wrong here, can I have some advise? Thanks!

2 Answers 2

3

Python functions always take the class or instance as the first argument of class functions/methods.

class Functions:
    def mapping(self, func, x):
        return func(x)

Alternatively, if you don't want to always create instances of the class, do the following to create a class method as opposed to an instance method.

class Functions:
    @classmethod
    def mapping(cls, func, x):
        return func(x)

The @ operator creates a function decorator, if it's a topic you'd like to google more.

Sign up to request clarification or add additional context in comments.

Comments

1

I couldn't make much sense of your code (it seems incomplete, and names don't match up: mapping -> map, stream -> testing), but from the error message, it seems like you need to construct an instance of Function first. So you need something like:

func = stream.Functions()
func.map(testdouble, [1, 2, 3, 4, 5])

You also need to declare the map/mapping method properly (after deciding whether you want an instance or class method), as Kurt pointed out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.