S
S
semolex2015-09-22 22:18:48
Python
semolex, 2015-09-22 22:18:48

How not to repeat a certain method every time a method is called?

Perhaps my wording is too abstract.
But here's the problem:
There is a class that represents the model (eg City, Street, House). It has methods that represent (that's right :)) methods of some API, for example:

class House(Api):
    def get_house_address(self):
        self.method('getAddress')
        return self.process(self.data) #parent method
    def get_neighbourhoods(self):
         self.method('getClosestHouse')
        return self.process(self.data)

Actually here - `self.method` confuses me, which I call every time. This method changes the fields in `self.data` to send the correct request to the API. This is easier than writing a huge query every time, when it is enough to replace the fields, but this option does not look nice either.
How right is it to do it? Is there a way to make it prettier?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Smirnov, 2015-09-23
@semolex

I will answer a little differently.
You most likely don't need to do this.
You don't have to do this because your code is clean, readable, and easy to maintain.
And before you do anything, you should answer the question why you are not satisfied with the current code.
What characteristics of the code do not suit you, that you want to change them?
If you are still able to answer this question, then maybe you should dig in this direction:
stackoverflow.com/questions/6307761/how-can-i-deco...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question