1

I have tried to produce a simplified version of what I am trying to accomplish; here it is:

class CustomString():
    def __init__(self, function):
        self._creator_function = function

    @property
    def string(self, *args):
        return self._creator_function(*args)


def creator1(arg1):
    return 'Hello ' + arg1

def creator2(arg1, arg2):
    return 'Aloha ' + arg1 + ',' + arg2

The CustomString class is meant to be a dynamic string generator, which is provided a function on initialisation that performs that returns the string given specific arguments. Those functions might take an explicit number of arguments, as in the example below, but the class method should be flexible because each instance might have a different creator function.

If I run the following:

c = CustomString(creator1)
c.string('John')

I would expect to see:

'Hello John'

But I get an error instead:

TypeError: creator1() missing 1 required positional argument: 'arg1'
1
  • 2
    Why is string a property? Commented Jan 27, 2020 at 15:19

1 Answer 1

2

Remove property from string

class CustomString():
    def __init__(self, function):
        self._creator_function = function

    def string(self, *args):
        return self._creator_function(*args)


def creator1(arg1):
    return 'Hello ' + arg1

def creator2(arg1, arg2):
    return 'Aloha ' + arg1 + ',' + arg2

c = CustomString(creator1)
print(c.string('John')) # prints Hello John
Sign up to request clarification or add additional context in comments.

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.