A function inside a function Python

2024/11/15 12:49:49

This is a problem asked before but I can't really understand the other explain of this kind of problem so I'm here to re-write it in more details. While studying I have encountered this kind of code that I am not at all familiar .. I can not understand how to interpret this g() function in f() function ! Why the piece of code inside g() where x = 10 and y = z*w does not run ? It's only print me the value of y I gave, calling f() with 5 !

x = 99def f(y):w = x + ydef g():x = 10y = z * wprint yf(5)
Answer

In Python, def is a statement: takes a function name, possibly arguments, then an indented function body -- compiles it all into a function object, which it binds to the given name in the scope where def appeared (here, locally to f).

So you ask "Why the piece of code inside g() where x = 10 and y = z*w does not run" -- very simply, because you never call g!

The fact that g is local to f (or as is also known "nested in f") is not germane.

Whether local or global, anytime you def g but then never call g, the code in g's body will not execute.

Incidentally, this is a detail in which Python coincides with every other language I've ever heard about. If a function is defined (some languages call that "declared") and never called, then the function's body code never runs. Have you ever heard of any language doing otherwise -- i.e, executing the code body of a function that's defined but never called?!

https://en.xdnf.cn/q/119054.html

Related Q&A

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …

SOLVE: AttributeError: ImmutableDenseNDimArray object has no attribute as_independent

I am quite new to python and I have been trying to plot this in a few different ways. If I try using np.vectorize, it crashes. so i wrote this code, which is giving me the error in the title: import ma…

Python memory limit [duplicate]

This question already has answers here:In-memory size of a Python structure(7 answers)Closed 10 years ago.So clearly there cannot be unlimited memory in Python. I am writing a script that creates lists…

Automating Gmail login in Python

I am writing a Python program that can login Gmail.The purpose of this program is to check whether the username/password combination exists and is correct.Since this program is to test the the username…

Reversing order in incrementing digits

I have a list of numbers, and Im trying to do the following in a way as efficient as possible.For each consecutively incrementing chunk in the list I have to reverse its order.This is my attempt so far…

why python selenium get the empty page_source?

I try to simulate the buy item operation on the link below. (Need login in first)taobao_item_linkAnd after you click the button below. img_link:The link will jump to a new link.But if I print out the p…

How do i print the repetition output using regex it prints only first match

I have task where I need to print the occurrence and count and non-occurrence and count import resequence = 1222311m = re.search(r(\d)\1+,sequence)print(m) Exptected output : (1, 1) (3, 2) (1, 3) (2…

How do I get rid of all roles of the user discord.py

I was making my mute command and I thought of getting rid of all the members role but I dont know how to get rid of all the members roles I even tried for role in member.roles:await member.remove_roles…

How to pick the rows which contains all the keywords? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

Extract HTML Tables With Similar Data from Different Sources with Different Formatting - Python

I am trying to scrape HTML tables from two different HTML sources. Both are very similar, each table includes the same data but they may be structured differently, with different column names etc. For …