I need to create an instance specific URL based on the argument given to create the instance. This URL has to be available to all methods of my class, but I don't want the URL to be an attribute of the instance itself.
This is what I have:
class Person(object):
def __init__(self, name):
self.name = name
self.url = f'https://stackoverflow/{name}/'
def methodA(self):
self.result1 = parse(self.url, do sth)
def methodB(self):
self.result2 = parse(self.url, do sth else)
This would be an improvement but wouldn't fulfill the DRY principle:
class Person(object):
def __init__(self, name):
self.name = name
def methodA(self):
url = f'https://stackoverflow/{self.name}/'
self.result1 = parse(url, do sth)
def methodB(self):
url = f'https://stackoverflow/{self.name}/'
self.result2 = parse(url, do sth else)
Isn't there something in between?
I thought about defining a method which deletes unwanted runtime attributes after adding them to self, but that's probably not best practice?
For the context: The example above is heavily simplified. The real world example is about several parsed objects of the response which are being used multiple times.