0

I am new to Python. I am attempting to match some data, although I need to "clean" the data before matching it.

I currently have 1 class for matching and one for cleaning.

When using the cleaning class in matching, should I use inheritance or class methods? Both will work but which is more 'pythonic'.

Option 1

import DataCleaner

class DataMatcher:

   def clean(self):
       self.name = DataCleaner.clean_name(self.name)   

Options 2

import DataCleaner

class DataMatcher(DataCleaner):

   def clean(self):
       self.name = self.clean_name()
  • Aside from the semantic issues with the second approach, you should consider that it doesn't even work. – user2357112 Aug 04 '16 at 00:21
  • 1
    Are you coming from Java or something? You're using classes where they don't seem to be helpful, and you're trying to import classes with the syntax used for modules. – user2357112 Aug 04 '16 at 00:22
  • @user2357112 - Why do you say option 2 wouldn't work? I don't have a programming background. I am new to programming in general. I've been advised to use class as the preferred way to implement code. – Mark Jones Aug 04 '16 at 04:53
  • The call to `self.clean_name()` inside the `clean_name` you defined is going to call the method you defined, not the one from `DataCleaner`. – user2357112 Aug 04 '16 at 05:18
  • Thanks, I understand. I have edited. Does this make more sense? – Mark Jones Aug 04 '16 at 05:29
  • It won't automatically blow up in your face any more, but the first option is still much better, and you still ought to consider not having a `DataCleaner` class at all. – user2357112 Aug 04 '16 at 05:51
  • Thanks - The methods in DataCleaner are used in other classes throughout the project. How would I implement these? I have been reading http://stackoverflow.com/questions/19620498/how-to-create-an-utility-class-correctly - Should I be doing a util file with global functions? – Mark Jones Aug 04 '16 at 06:13
  • Defining a bunch of cleaner functions in a `datacleaner` module would be a sensible way to go about it. – user2357112 Aug 04 '16 at 06:32

0 Answers0