Accessing a parameter passed to one function in another function

2024/11/15 12:28:45

I have two functions:

def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something else

p1 and p2 are the same data, so I want to avoid asking for the input twice. Is there a way I can pass the argument supplied to f1 to f2 without asking for it again? Ideally I would be able to use something like you would in a class. Like f1.p1Is this possible?

EDIT: To add some clarity, I looked into using the ** operator to unpack arguments and I'm aware that using the main body of the program to access the arguments is cleaner. However, the former does not match what I'm trying to do, which is gain a better understanding of what is accessible in a function. I also looked at using the inspect and locals, but these are for inspecting arguments within the function, not outside.

Answer

Yes, depending on your needs. The best would be to ask in the main program, and simply pass that value to each function as you call it. Another possibility is to have one function call the other.

# Main program
user_input = raw_input("enter data")
f1(user_input)
f2(user_input)

Ideally I would be able to use something like you would in a class.Like f1.p1 Is this possible?

That's an advanced technique, and generally dangerous practice. Yes, you can go into the call stack, get the function object, and grab the local variable -- but the function has to be active for this to have any semantic use.

That's not the case you presented. In your code, you have f1 and f2 independently called. Once you return from f1, the value of p1 is popped off the stack and lost.

If you have f1 call f2, then it's possible for f2 to reach back to its parent and access information. Don't go there. :-)

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

Related Q&A

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 …

AttributeError: NoneType object has no attribute replace_with

I am getting the following error:Traceback (most recent call last):File "2.py", line 22, in <module>i.string.replace_with(i.string.replace(u\xa0, -)) AttributeError: NoneType object has…