python: passing parameters through a functions tree
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
python: passing parameters through a functions tree
Hi,
I have a functions tree in which a requests session should be opened at the top of the tree, but the session itself should be used by the functions in the bottom of the tree.
please consider this simple example:
Code:
def scrape(sites_list):
for site in sites_list:
with requests.Session() as req_session:
funcA(site, req_session)
def funcA(site, req_session):
funcB(req_session)
def funcB(req_session):
req_session.get('site.com')
To let funcB() use req_session should I pass it as a parameter through the whole functions tree as show above? Or is there a smarter way to do it?
for what i understand, the with construct would automatically close each session in case of error.
could i get something similar with a try/except construct inside main()?
I don't really understand the question. And also I don't understand why don't you try your idea. Would be better to try to implement what you want and discuss the practical details.
I don't really understand the question. And also I don't understand why don't you try your idea. Would be better to try to implement what you want and discuss the practical details.
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
2) even if it seems to works, it doesn't mean it's the right way
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
Actually I don't know which wheel is it
Quote:
Originally Posted by masavini
2) even if it seems to works, it doesn't mean it's the right way
there is no "right way", there are several different ways available to implement anything. So your solution can be acceptable especially because you understand how does that work.
From the other hand you can show us that code and we can help you to improve it.
well, i don't know either. but as i wrote i'm a newbie and i just know there are A LOT of wheels: i didn't want to miss an important one.
Quote:
Originally Posted by pan64
there is no "right way", there are several different ways available to implement anything. So your solution can be acceptable especially because you understand how does that work.
From the other hand you can show us that code and we can help you to improve it.
thanks, i usually post code samples to avoid bothering people with not relevant (frequently badly written) code...
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
2) even if it seems to works, it doesn't mean it's the right way
Both good attitudes to have, but as Pan says there isn't a single right answer, sometimes you have to make a choice and it'll either be the right one or you'll learn something.
Different approaches can work better is different situations, but sometimes they work equally well and it doesn't matter which one you use - what's more important is consistency, correctness, and readability of the code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.