I'm writing an program where im using two main functions however those both functions uses same inner functions. I'm wondering how I should write them in most pythonic way? My point is to hide those helpers somewhere inside and to dont repeat helper functions.
def main_function1():
helper1()
helper2()
#dowork1
def main_function2()
helper1()
helper2()
#dowork2
def helper1()
#workhelp1
def helper2()
#workhelp2
The only reasonable solution which i can figure out is declaring static class with.. private functions? But since:
Strictly speaking, private methods are accessible outside their class,
just not easily accessible. Nothing in Python is truly private[...]
Im stuck and out of ideas.
From: http://www.faqs.org/docs/diveintopython/fileinfo_private.html
Topic: Why are Python's 'private' methods not actually private?
Also I thought about declaring one main function with inner helpers and with switcher as a argument to determine which function should run but I guess thats pretty poor solution.
For now only way I find the most accurate is to declare normal class as:
class Functions:
def main_function1(self):
print("#first function#")
self.helper1()
self.helper2()
def main_function2(self):
print("#second function#")
self.helper1()
self.helper2()
def helper1(self):
print("first helper")
def helper2(self):
print("second helper")
Functions().main_function1()
Functions().main_function2()
print("###")
Functions().helper1()
Output:
#first function#
first helper
second helper
#second function#
first helper
second helper
###
first helper
But also here i can access helpers which isnt a big deal but still gives me a reason to wonder.