I would like to ask about usage of static methods inside other methods in python class. I don't know where should i post it, but meta.stackexchange recommended to post it here.
What i want to do is to build StringProcessor
class, which should preprocess a string.
I want to start with basic functionality(like deletion of bad characters, strip etc.) but later the plan is to extend basic functionality.
First variant of the class:
import re
class StringProcessor:
"""
This class is responsible for the string preprocessing.
"""
def __init__(self,bad_chars = None):
self.bad_chars = bad_chars
def link_processor(self,string):
#preprocessing of the link
return string
def name_surname_processor(self,string):
#manipulation with names and surnames
return string
def delete_bad_characters(self,string):
if self.bad_chars is not None:
return string.translate(self.bad_chars)
return string
def standard_processor(self,string):
string = self.delete_bad_characters(string)
string = self.link_processor(string)
string = string.strip()
string = string.lower()
return string
So here I am stick to the instance methods.
Here is the second variant of the class:
class StringProcessor:
"""
This class is responsible for the string preprocessing.
"""
def __init__(self,bad_chars = None):
self.bad_chars = bad_chars
@staticmethod
def link_processor(string):
#preprocessing of the link
return string
@staticmethod
def name_surname_processor(string):
#preprocessing of the name and surname
return string
return string
def delete_bad_characters(self,string):
if self.bad_chars is not None:
return string.translate(self.bad_chars)
return string
def standard_processor(self,string):
string = self.delete_bad_characters(string)
string = self.link_processor(string)
string = string.strip()
string = string.lower()
return string
The idea is, that standard_processor()
is a main method, which uses other helper methods(like link_processor
) for the string preprocessing. My question is: in this case is it better to use @staticmethod
for the helper methods of it's better to stick with regular instance methods?