Can i access python function inside in same function? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can i access python function inside in same function or sub function of main function?
def main_function():
def sub_function():
main_function() # i need to call main function.can i or not.any solution?
NameError: name 'main_function' is not defined

You can do that, yes.
I don't see anything wrong with the code you posted. But, it will not actually do anything, because you can only call sub_function from within main_function and currently you are only defining sub_function but not actually calling it. If you got a NameError somehow, despite your code not actually executing anything in any real way, there must be some other reason.
If you do it like this, which is similar to what you did except it adds an actual call to sub_function inside main_function:
def main_function():
def sub_function():
main_function()
sub_function()
and then you call main_function(), you will get a Maximum call stack size exceeded error, because it is an infinitely recursing function.

Related

Declare variables types for python function triggers invalid syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am having trouble why I am getting an invalid syntax error when I declare what my variable is inside my function. The function works when I step through it in python, but not when I try to run the program from the command line.
command line code:
python filename.py
python code:
import pandas as pd
def my_func(df:pd.DataFrame,name:str):
print("Hello world")
def main():
my_func(df=pd.DataFrame(),name='name')
if __name__ == "__main__":
main()
Your code is perfectly valid. Check your Python version - you're probably using a version that doesn't support type hints, such as Python 2.
(Perhaps python resolves to python2?)

Getting TypeError: 'list' object is not callable while using Map method in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
s = [1,2,3,4]
list(map(lambda parameter : parameter*2, s))
I am trying to run this simple line of code, but ending up getting
TypeError: 'list' object is not callable
can anybody please suggest me a solution for this
Your code works as is. I'm guessing (for lack of info) that somewhere above these lines of code you've overridden list as an actual list...
Check your code for this kind of error, and if it exists then refactor and rename so as to not use list as a parameter in your code.

Class instantiation with and without parantheses [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Consider the following:
class car:
pass
tesla=car()
When I do type(tesla), this gives __main__.car, as I expect.
But if I have:
class car:
pass
tesla=car
then type(car) gives type.
From another post here on SO, it really shouldn't matter how I instantiate the class. But clearly as we see here, the type of the object is different. I am an absolute beginner to python, could you please let me know what is going on here.
Thanks!
In the second case, you're not instantiating the class, just giving it another name, tesla. The parentheses are required for instantiation.

What is the purpose of "_104" in round function in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
In round function what is the use of _100, _104, etc.
I know that round(10.121, 1) will round the value up to 1 decimal place.
But the purpose of _104 is not getting.
I've attached two screenshots for reference.
In IPython, _109 means the output of the 109th cell execution. This implies that while you were running your IPython notebook (through VS Code), at cell execution 109, you received an output of 103. The reason why Bram above can't run print(_109) on his end is that he was either running it in a Python file/interpreter, or executing it in a fresh notebook where he hasn't run 109 cells yet, and thus the variable _109 did not exist.
Check this answer for more information: https://stackoverflow.com/a/27952661/2327379

Python def marked as invalid syntax [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Im working on a (in-shell) geometry calculator in Python, and I get a syntax error everytime marked as the def in the below code:
def scepm(r,h):#surface-area circular-prism(cylinder)comment s.o.f. only
print(3.14159265358979323846264338327950*r**2+3.14159265358979323846264338327950*r*h)
It might be something obvious. If it is, can someone please point it out? thanks
If the syntax error is indicating the def it means that def is not valid at this point in the program. def starts a statement, so the conclusion would be that you are not starting a statement at the beginning of the line, you must have unclosed parentheses in the previous non-blank, non-comment line.
That is assuming your description is accurate: if not you could get other errors such as an indentation error, or if it indicated somewhere later in the line it could be some other issue you failed to copy exactly.
It works fine you should call the function though:
def scepm(r,h): # surface-area circular-prism(cylinder)comment s.o.f. only
print(3.14159265358979323846264338327950*r**2+3.14159265358979323846264338327950*r*h)
scepm(1,3) #function call

Categories

Resources