python - making HTTP request upon class initialization -
new programming, may stupid question. if so, apologies.
i'm writing class in python on initialization makes request api rather large amount of data:
class foo: def __init__(self, params): self.params = json.dumps(params) self.r = requests.get(api, data=self.params).json() after initialization, there bunch of methods — bar , baz — meant, in ideal world, perform different operations on the json fetched upon initialization.
when instantiate class , call methods in succession, this:
test = foo() test.bar() test.baz() my naive understanding test.bar() , test.baz() performing operations on same json fetched upon instantiation of test , not making own api calls. obviously, whole thing slower if test.bar() , test.baz() each making own requests. understanding correct? or test.bar() , test.baz() fetching own copies of json data?
it entirely depends on content of bar , baz functions. could call requests.get if that's how write them.
if you're wondering whether or not __init__ function called when call test.bar(), answer no, not. __init__ called when instantiate foo object.
Comments
Post a Comment