Why has this value not changed?

class W ():

headers = {"user-agent": UserAgent().random,}

def a(self):
    print(self.headers)
    pass

def b(self):
    print(self.headers)
    print(self.headers)
    pass
pass

aa = W ();
aa.b ()
aa.b ()

results
{"user-agent":" Mozilla/5.0 (X11; Linux x86"64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"}
{"user-agent":" Mozilla/5.0 (X11; Linux x86"64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"}
{"user-agent":" Mozilla/5.0 (X11; Linux x86 / 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"}
{"user-agent":" Mozilla/5.0 (X11; Linux x86 / 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"}
Why aren"t the results random?

Mar.14,2021

because of your

class W:
    headers = {"user-agent": UserAgent().random,}

will only be executed once when the class is defined, and should be replaced by

class W:
    def make_new(self):
        return {"user-agent": UserAgent().random,}
Menu