ENH: Session constructor should acccept kwargs as well, not just its get, post... methods

The documentation states that things like proxies, auth etc. can be set through the Session properties. That's fine, but leads to verbose code: Setting those properties when you have a kwargs dict requires a for loop and call to setattr() on each item.

Why not allow the following:

s = requests.Session(stream=True, proxies=myproxies)

or

s=requests.Session(**myhttp_config)

Am I missing something?


Removing the arguments to the `Session` constructor was a deliberate decision made by @kennethreitz in 92355ada54a3a19341f7fbc65a8bc50816858c63. In this issue I cannot pretend to speak for him, but I believe in his view this was done for cleanliness (which seems reasonable to me). We have no plans to change that portion of the API at this time. Sorry! :smile:


To be fair, you can do this yourself.

``` py
def make_session(**with_config):
    session = requests.Session()
    for attr, value in with_config.items():
        if hasattr(session, attr):
            setattr(session, attr, value)
    return session

session = make_session(proxies={'http': 'http://10.9.8.7:9191/'}, headers={'Accept': 'application/json'})

# Alternatively

class Session(requests.Session):
    def __init__(self, **config):
        super().__init__()
        for attr, value in config.items():
            if hasattr(self, attr):
                setattr(self, attr, value)
```

Of course, this doesn't let you set anything that the session doesn't already have defined, but that seems like a fair trade-off that you can ignore if you write this yourself. That said, there were some complaints about this shortly after we removed the functionality, but this is the first complaint we've had in years that it's not there. That indicates that it isn't something that a lot of people are having trouble with it.


Thanks. Yes, it's no big deal to write a factory function or wrapper
class for this. Even so, simple-minded humans like me will scratch their
heads wondering how this is cleaner than simply accepting kwargs to
initialize a Session.

But I am sure Kenneth will have a good reason for removing this feature.
And now that I know I'm not alone looping over items and setting
attributes, I will delightfully loop on.

Thanks for an excellent library,

Leo

Am 18.05.2015 um 20:57 schrieb Ian Cordasco:

> To be fair, you can do this yourself.
> 
> def make_session(**with_config):
>     session = requests.Session()
>     for attr, value in with_config.items():
>         if hasattr(session, attr):
>             setattr(session, attr, value)
>     return session
> 
> session = make_session(proxies={'http': 'http://10.9.8.7:9191/'}, headers={'Accept': 'application/json'})
> 
> # Alternatively
> 
> class Session(requests.Session):
>     def **init**(self, **config):
>         super().**init**()
>         for attr, value in config.items():
>             if hasattr(self, attr):
>                 setattr(self, attr, value)
> 
> Of course, this doesn't let you set anything that the session doesn't
> already have defined, but that seems like a fair trade-off that you
> can ignore if you write this yourself. That said, there were some
> complaints about this shortly after we removed the functionality, but
> this is the first complaint we've had in years that it's not there.
> That indicates that it isn't something that a lot of people are having
> trouble with it.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/kennethreitz/requests/issues/2605#issuecomment-103176002.