Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most pythonic way to declare inner functions

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.

like image 245
sobczi Avatar asked Mar 24 '19 03:03

sobczi


People also ask

How do you define a nested function in Python?

A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.

Can you define functions inside functions Python?

If you define a function inside another function, then you're creating an inner function, also known as a nested function. In Python, inner functions have direct access to the variables and names that you define in the enclosing function.

Are closures Pythonic?

Understanding what, when & why to use closures!Closures are elegant Python constructs. In this article, we'll learn about them, how to define a closure, why and when to use them. But before getting into what a closure is, we have to first understand what a nested function is and how scoping rules work for them.


1 Answers

There are no private functions in Python. Rather, by prefixing the names of methods intended to be non-public with underscores, you signal to users of your class that those methods are not meant to be called externally:

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")

This is in line with the principle of "We're all consenting adults here" - you can touch the non-public methods of a class, but if you use them wrongly, that's on your own head.

like image 58
gmds Avatar answered Sep 21 '22 10:09

gmds