kinishinai kinishinai - 1 year ago 150
Python Question

Why does cookielib.LWPCookieJar accept an empty argument list?

In the cookielib documentation, the definition of cookielib.LWPCookieJar is:

class cookielib.LWPCookieJar(filename, delayload=None, policy=None)

The filename is a required argument.

But in the following python code,
has no argument, yet still works.

import cookielib
cookie = cookielib.LWPCookieJar()

Who knows why?

Answer Source

Because the documentation is lying, apparently. imports the definition of LWPCookieJar from, which defines it as:

class LWPCookieJar(FileCookieJar):

FileCookieJar is defined in, as:

class FileCookieJar(CookieJar):
    """CookieJar that can be loaded from and saved to a file."""

    def __init__(self, filename=None, delayload=False, policy=None):

So the filename argument is actually optional (and delayload defaults to False, not None).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download