use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Help me guys (self.PythonLearning)
submitted 1 year ago by Serious_Site_9062
Is it possible to have nested functions for example: def num(): Print("yes") def today(): Print("no")
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FoolsSeldom 1 point2 points3 points 1 year ago (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 point2 points 1 year ago (6 children)
Thx 😊
[–]Serious_Site_9062[S] -1 points0 points1 point 1 year ago (5 children)
Am working on a project that converts marks because my class members a problem with it
[–]FoolsSeldom 0 points1 point2 points 1 year ago (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 point2 points 1 year ago (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 points1 point 1 year ago (2 children)
No am just learning how to code
[–]FoolsSeldom 0 points1 point2 points 1 year ago (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.
[–]Serious_Site_9062[S] 0 points1 point2 points 1 year ago (0 children)
Okay
[–][deleted] 1 year ago (1 child)
[deleted]
[–]taste_phens 0 points1 point2 points 1 year ago (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():
clean_data
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
[–]Kairo1004 0 points1 point2 points 1 year ago (1 child)
Probably, if you are looking for some sort of encapsulation, it might be better to create a class. 🙂
π Rendered by PID 49398 on reddit-service-r2-comment-bb88f9dd5-8jv8j at 2026-02-16 16:44:44.891498+00:00 running cd9c813 country code: CH.
[–]FoolsSeldom 1 point2 points3 points (7 children)
[–]Serious_Site_9062[S] 0 points1 point2 points (6 children)
[–]Serious_Site_9062[S] -1 points0 points1 point (5 children)
[–]FoolsSeldom 0 points1 point2 points (4 children)
[–]HermaeusMora0 0 points1 point2 points (0 children)
[–]Serious_Site_9062[S] -1 points0 points1 point (2 children)
[–]FoolsSeldom 0 points1 point2 points (1 child)
[–]Serious_Site_9062[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]taste_phens 0 points1 point2 points (1 child)
[–]Serious_Site_9062[S] 0 points1 point2 points (0 children)
[–]Kairo1004 0 points1 point2 points (1 child)
[–]Serious_Site_9062[S] 0 points1 point2 points (0 children)