tags:

views:

95

answers:

3

Hi, I have original class (DownloadPage) and I need to add just one simple functionality (get_info). What is better approach in OOP?

def get_info(page):  # make simple function
    ...
result = get_info(DownloadPage())

or

class MyDownloadPage(DownloadPage):   # make new class with inheritance
    def get_info(self):               # with one method
        ...

result = MyDownloadPage().get_info()

Thanks

+2  A: 

Since the distance from one solution to the other is small, it hardly makes a difference with one function.

As soon as you have two functions, I'd say the derived class is more maintainable.

Once the two functions want to share a common value, like a logger or configuration variable, then you'll see more benefit.

Gabriel
+2  A: 

The answer truly depends on whether or not you want that get_info function/method can function on things other than a MyDownloadPage. At present, I'd go with the free function but when requirements solidify one way or the other it should be easy enough to transform your solution either way.

(I prefer the free function because it doesn't restrict what can be passed to it, and any functionality that relies only on another object's public interface should be a function to reduce coupling.)

dash-tom-bang
"get_info function/method can function on things other than a MyDownloadPage". It can't. So subclassing should be better choice.
Vojtech R.
Not necessarily- if all of that information is needed by the get_info function is available via the `MyDownloadPage` public interface, there's no need to make it a member function.
dash-tom-bang
A: 

This may be heretical, by why not just add it as a method to the existing class?

def get_info(self):
    ...

DownloadPage.get_info = get_info

I realize this isn't exactly standard practice, but I'd be curious to hear Python experts explain why not.

munificent
Looks like we got a Ruby user here!
chpwn
I think it may be confusing for somebody because "this isnt't exactly standard practice". I tend to use standard practices.
Vojtech R.