What Does It Take To Be An Expert At Python?
Why Stack Overflow recommends it
Stack Overflow users link it as a reference when explaining decorators, generators/yield and Python's protocol-style data model.
Recommended by 3 people across 3 different months, 2018 to 2020.
What it is
A nearly two-hour PyData 2017 talk by James Powell on what separates expert Python programmers from the rest. It covers the Python data model and dunder methods, decorators, generators and context managers, with live coding. Coding Tech republished it with permission from the organizers.
What people said
Recommends J.doe · 2018-11 · Stack Overflow · on “Can Python objects call methods that are lower in the inheritence stack?”
I am trying to watch a video on python tricks shown here [this video] . He is trying to demonstrate when using a higher level class when calling a method provided by the developer extending the class, that he can assure the method is written. He then wrote two classes in two different files -- library.py class Base(): def foo(self): return self.bar() -- user.py from library import Base class Derived(Base): def bar(self): return 'bar' Now, I am confused why this would even work since the class Base , cannot call methods of anyone that inherits from it. For example, the following code will produce an error. class Higher(): def higher_method(self): print("higher method") class Lower(Higher): def low
Recommends Poojan · 2019-01 · Stack Overflow · on “Is it a good practice to assign atributes to the tester class that will be used by other tests”
You can achieve desired behavior using yield. Which can be used to maintain sequence of executions. def foo_1(): #load file and do stuff return bar_1 def foo_2(arg1): #do stuff with arg1 return bar_2 def foo_3(arg1, arg2): #do stuff with arg1 and arg2 return bar_3 def run(): arg1 = foo_1() yield arg2 = foo_2(arg1) yield arg3 = foo_3(arg1,arg2) yield for steps in run(): pass Check out this resource where he explained in detail how it works. [this video]
Recommends Allan Lago · 2020-02 · Stack Overflow · on “How to change the error message for all exceptions in Python?”
I am not sure if its possible to elegantly change all exception messages. Here's the next best thing I could come up with. We are going to use decorators. In general, decorators are like wrappers for functions. There is a good explanation for how they work here: [this video] This is the one I came up with: def except_message(message=''): def inner(f): def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except Exception as e: raise type(e)(str(e) + "\n" + message).with_traceback(sys.exc_info()[2]) return wrapper return inner Atop a function that you want to use this decorator on, write @except_message(message='My_message') where 'My_message' is whatever you want the message to be. (It wil
Cites DocDriven · 2018-08 · Stack Overflow · on “Using Protocol-Oriented Programming (POP) in Python”
I recently came across a very interesting talk by James Powell on YouTube ( LINK [this video] , around 12 minutes into the video). In his PyData talk, he states that "[...] The Python data model is a means by which you can implement protocols [...]". He relates his statement to the use of dunder-methods (like __init__ , __repr__ , etc.). I've been programming for quite a while with Python, but I haven't come across POP. The video didn't really define what it is. What is POP in Python actually and how is it used in practice?
Cites Sofien · 2019-10 · Stack Overflow · on “How does nested function work for decorator?”
I would recommend to use a class to achieve your goal. Here are my approaches: If you want to execute the function only once no matter waht arguments you pass: class ExecuteOnceClass: called = [] @staticmethod def execute(function): def wrapper(*args, **kwargs): if function.__name__ in ExecuteOnceClass.called: print("Function already called once") else: function(*args, **kwargs) ExecuteOnceClass.called.append(function.__name__) print("called function") return wrapper @ExecuteOnceClass.execute def hello(name): print(f"Hello {name}.") hello("Neuneu") hello("Sofian") Expected output: Hello Neuneu. called function Function already called once If you want the function to get called once with a vi
Cites StefanMZ · 2020-03 · Stack Overflow · on “Continue for loop after caught Exception”
What python version are u using? Also why not using yield ( see enter link description here [this video] ) You can return instead of raise. If you know if type(variable) is Exception you cand process information.