2

Is there a nice solution to do something like this lambda function in a parameter list?

timeout = None

[...]

 response = self.session.post(
        url=self.baseUrl,
        data=str(data),
        headers=headers,
        timeout=lambda: 0 if self.timeout is None else self.timeout
    )

I know it throws an exception. But is there a possible solution to do something like this?

Thanks

3
  • 11
    It's not clear what you are doing. That code by itself would not throw an exception, it's perfectly valid; but presumably the post method is not expecting a callable, so I don't understand why you are passing it one. Why can't you simply do timeout=0 if self.timeout is None else self.timeout? Commented Feb 9, 2015 at 12:33
  • Agree with @DanielRoseman. You don't seem to gain any advantage here using lambda. Is this your real code or just an example you're using for the question? Commented Feb 9, 2015 at 12:36
  • Thank you @DanielRoseman. It was just an example code. Of course i should do timeout=0 if self.timeout is None else self.timeout . I just was wondering if there is a possibility to use the return of a function in a parameterlist Commented Feb 9, 2015 at 12:41

2 Answers 2

1
timeout = None

 response = self.session.post(
        url=self.baseUrl,
        data=str(data),
        headers=headers,
        timeout=(lambda: 0 if self.timeout is None else self.timeout)()
    )

It is just an inline function works with closures.

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

Comments

0

If you define timeout as class variable, than it is something determining object's state. In case where you have to set initial value depending on some condition you can do one of folowing basic things:

  1. Initialize it in __init__

  2. Use @property decorator and define getter, which returns applicable value.

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.