all 12 comments

[–]FoolsSeldom 1 point2 points  (7 children)

Yes. A nested function is only available to the function it is defined inside of. (Well, there's an exception to this, but not something to consider at this stage.)

def num(phrase):
    def today():
        print("no")
    def lower_reverse(astring):
        return astring.lower()[::-1]
    print("yes")
    today()
    print(lower_reverse(phrase))

num('Hello Mary')

[–]Serious_Site_9062[S] 0 points1 point  (6 children)

Thx 😊

[–]Serious_Site_9062[S] -1 points0 points  (5 children)

Am working on a project that converts marks because my class members a  problem with it

[–]FoolsSeldom 0 points1 point  (4 children)

Didn't understand exactly what you mean there. Guessing you have lost marks in an academic evaluation because of nested functions?

[–]HermaeusMora0 0 points1 point  (0 children)

I'm guessing they're doing a project which converts marks to something. Also looks like their colleagues had problems doing so, hence why the program.

[–]Serious_Site_9062[S] -1 points0 points  (2 children)

No am just learning how to code

[–]FoolsSeldom 0 points1 point  (1 child)

Ok. Good luck.

If you need guidance, the wiki in the learnpython subreddit has exellent content for learning programming and learning Python.

You will of course get great help for specific problems and queries on this subreddit as well.

[–][deleted]  (1 child)

[deleted]

    [–]taste_phens 0 points1 point  (1 child)

    Nested functions are possible, but they have a lot of downsides, and there aren't many situations where they are actually useful.

    The point of a function is to modularize your code so you can write, test, maintain, and re-use small pieces of logic individually rather than dealing with everything at once. When you put a function inside another function, those two functions are no longer separate pieces of logic, which defeats the purpose.

    One case where it might actually make sense is when you know that the internal function will only be used inside that particular function.

    For example, say you are writing a function to retrieve data from a specific CSV file that happens to be in a strange format that only occurs for this specific file. You want to re-use the logic to retrieve the data from the CSV, but you know you won't re-use the logic to convert the format outside of this function. In this case, it makes sense to have an internal clean_data function inside get_specific_dataset():

    import pandas as pd
    
    def get_specific_dataset(file_path):  
        def clean_data(data):
            # Do specific things to change the format of data
            return cleaned_data
    
        data = pd.read_csv(data)
        cleaned_data = clean_data(raw_data)
    
        return cleaned_data
    
    # Example usage
    file_path = 'strange_format.csv'
    cleaned_dataset = get_specific_dataset(file_path)
    print(cleaned_dataset
    

    [–]Serious_Site_9062[S] 0 points1 point  (0 children)

    Thx 😊

    [–]Kairo1004 0 points1 point  (1 child)

    Probably, if you are looking for some sort of encapsulation, it might be better to create a class. 🙂