0

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.

1
  • 1
    The obvious approach would seem to be to provide the url in a dedicated method or property of your class. Commented Jul 4, 2021 at 18:40

2 Answers 2

1

In addition to making it private, as @plalx mentioned, it seems like you also want to make it dynamic relative to self.name, which you can do by making it a property. For example:

class Person(object):
    def __init__(self, name):
        self.name = name

    @property
    def _url(self):
        return f'https://stackoverflow/{self.name}/'

    def methodA(self):
        self.result1 = parse(self._url)

    def methodB(self):
        self.result2 = parse(self._url)
Sign up to request clarification or add additional context in comments.

Comments

1

Unfortunately there's no access modifiers in Python so you can't make the variable private to the class like you would in languages like Java for instance. However, you can still indicate that it's meant to be an internal variable by using the _ or __ prefixes.

Another option would be to nest your class declaration within an enclosing function which is an approach commonly used to create private scopes in JavaScript, but since the URL is dynamic based on the name you'd have to re-compute it every time:

def Person(name):
    
    def urlOf(person):
        return f'https://stackoverflow/{person.name}/'
    
    class Person(object):
        def __init__(self, name):
            self.name = name
    
        def test(self):
            print(urlOf(self));
    
    return Person(name)

Person('test').test()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.