Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Basically I'm a C# developer,
I know the way C# does, EventHandler, delegate, even...
but whats the best way to implement it on Python.
I think you should be able to use a function:
def do_work_and_notify(on_done):
// do work
on_done()
def send_email_on_completion():
email_send('joe#example.com', 'you are done')
do_work_and_notify(send_email_on_completion)
Functions (and even methods) in python are first-class objects that can be tossed around like anything else in the language.
This question is a lot like Python Observer Pattern: Examples, Tips? which has lots of great answers. There's even an implementation of C#-like events in Python.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
What are some acceptable use cases for using while true. I’ve heard that it’s not good practice to use while true and break. For simple use cases like let’s say a rps game, would it be worth using. If not, I’m considering to use boiler plate code.
You can use while(True) in such conditions when you need to make the code running until interrupted by the person running the code.
A use case can be fee submitting system. The program needs to be running for the entire day. There are a lot of other use cases too.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm sorry if I'm not asking this correctly. It may be due to a lack of understanding.
If I was explaining to someone how to understand of a piece of code that's been written by someone else..I would say something like:
"Ok so here we have this method getting called and it's taking one argument. Let's find the method declaration itself and see what it's doing and what it's doing to our argument."
Is there a concise way of describing looking at code backwards in a way? Following the sequence of events? Flow of execution? Flow/order of operations?
Thank you.
My professors refer to it as tracing a function. It might be linked to localisation (I'm in France and translating their word into English) or to a specific language, but that's what I got and it sounds right.
I think this bit of Python doc supports my proposed answer.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I made a package that I've refactored following Uncle Bob's clean code book.
The result: I have a ton of smaller functions from the giant functions I had before (as I'm still learning Python & best practices).
My question is this:
To keep things organized...
On functions you won't use other places...
is it better to use nested functions
def public_func():
def private_func():
# do abc
private_func()
# do xyz
or have a lot of smaller functions with the private function syntax?
def _private_func():
# do abc
def public_func():
_private_func()
# do xyz
As the Zen of Python says:
Flat is better than nested.
But also:
Simple is better than complex.
Complex is better than complicated.
Therefore, try to not use nested, but if it makes it more complicated, keep it simple.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Let's say I have this:
a = {1: __class}
Is there any alternative to this ugly piece a[1]() ?
Also, tried a[1].__call__(argument) but PyCharm complains about Expected type 'type', got X instead
It seems to me that it defeats this statement:
Explicit is better than implicit
I am not asking what is the correct form of doing it. I use this form, and I know is right. I only asked for any alternatives.
While there is nothing wrong with a[1](), if you really want to argue that it's not explicit enough...
the_callable_object_that_i_pulled_from_the_dictionary= a[1]
the_callable_object_that_i_pulled_from_the_dictionary()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Or is python not a good programming language for that? If it isn't good for that, then what programming language is?
Depends on the program. There are lots of different programs (Firefox, Wordpress, jQuery...) that utilize plugins and they all use different programming languages.
Look at the plugin documentation for the program you want to write a plugin for and it will tell you what languages you can write the plugin in.