0
import requests
class Poll(requests.session()):
    def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)

with Poll() as p:
# do stuff

>>TypeError: __init__() takes 1 positional argument but 4 were given

I don't understand why an error is being thrown. Doesn't *args take care of any extra positional arguments?

Daniel Q
  • 137
  • 1
  • 2
  • 11
  • This code defines a class, but doesn't actually create any objects of that class. Show us that code also. – John Gordon Oct 06 '18 at 02:19
  • And the class declaration seems odd: `class Poll(requests.session())` -- Do you mean `Poll` to inherit from the type that is _returned_ by calling `requests.session()`? – John Gordon Oct 06 '18 at 02:20
  • Edited. I just use a with statement. When I call the session object normally, I use "with requests.session() as s:". requests.session is a function that, when called, creates a session object. – Daniel Q Oct 06 '18 at 02:23
  • What `Poll()` is doing is calling the init on `requests.session()` because you're not calling super on the `session`, but the object the `session` returns. (In other words, subclass the class `requests.session`, not the object `requests.session()`) – alkasm Oct 06 '18 at 02:24
  • Ohh. Am I inheriting from an instance of the class instead of the class itself? – Daniel Q Oct 06 '18 at 02:25
  • Yeah, I think that's the problem. – alkasm Oct 06 '18 at 02:25
  • @AlexanderReynolds with that change, I get a new error: "TypeError: function() argument 1 must be code, not str" – Daniel Q Oct 06 '18 at 02:26
  • However if you want to use context managers, it's best to use `__enter__` and `__exit__` methods to return the session object. What four arguments are you sending in? – alkasm Oct 06 '18 at 02:27

1 Answers1

0

Found the issue. requests.session is a function that returns an instance of the class requests.Session.

Fixed code:

import requests
class Poll(requests.Session):
    def __init__(self):
         super().__init__()
Daniel Q
  • 137
  • 1
  • 2
  • 11
  • @DavisHerring: Indeed. Daniel, see https://stackoverflow.com/q/100003/1340389 for the difference. – Kevin Oct 06 '18 at 03:36