Python Q&A with #1 Stack Overflow Python Expert
Codementor Python expert and Stack Overflow legend Martijn Pieters joined us for a session in office hours to share his insights about Python programming. Here are several questions asked by our viewers, and hopefully you will find Martijn’s response useful as well!
The text below is a summary done by the Codementor team and may vary from the original video and if you see any issues, please let us know!
Object-Oriented Programming Composition (OOD) and How to use it
A composition is any containment relationship, so anything you put inside of something else is a containment. In inheritance, you create a hierarchy of objects that define behavior. Therefore, inheritance is like a vehicle that can be sub-classed (e.g. car, truck, plane, etc.) However, you will never sub-class a vehicle into a person, though a vehicle can contain people. To understand the difference between containment and inheritance, you can think of them in this way: a car can hold at most 6 people, and a bus can hold about twenty. You can change what a class (car or bus) contains, but you can’t change the class itself. Thus, although containment and inheritance share a relationship in this way, they are different concepts.
Is Mixin in Python a Legacy from Other Languages and is it a Necessary or Useful Feature?
I think mixin is an extremely useful feature due to its ability to create an additional class that defines a different aspect that doesn’t fit in the rest of your structure. The concept of mixing is somewhat like emergency vehicles in giving a set of classes a bit of extra functionality. For example, you can have a truck that is also an emergency vehicle and therefore needs a siren. However, not all trucks are emergency vehicles, and maybe other types of vehicles may also be emergency vehicles, such as motorcycles. Motorcycles are part of the police force, and some police even use bicycles these days, but not all motorcycles or bikes are emergency vehicles. Mixin classes can definitely help with this sort of situation.
Can Python Optimization Work on Classes with no Assigned Variables?
Python cannot optimize classes that don’t have assigned variables, as it cannot know what your class names refer to until you run the program. Class names themselves are dynamic, so the code could in principle swap out those names into something else. Python Optimization only works on immutable objects.
Will CPython move from using a stack interpreter to a register-based interpreter to make the use of Python on resource-constrained devices like smartphones more feasible?
Background: Python bytecode currently uses a stack, which means it uses a piece of memory where two recent values are pushed onto the stack. Bytecode takes off the top one or two values, and replaces the top of the stack with the results of the expression (e.g. In the command 1+2, 1 and 2 goes into the stack. Bytecode takes them, adds them up, and pushes back 3 onto the stack. The next bytcode may store 3 into a variable or send it into a function.) On the other hand, a register-based interpreter uses a limited number of registers and stores the value in there instead of getting things from a stack, so it has to address any of your bytecode as registers. The advantages are that you can reuse register values, and just-in-time compilers (JIT compilers) can use registers more efficiently than stack interpreters. Thus, it is possible to control memory-use on register-based interpreters. However, a stack is easier to code.
I am not actually a part of the core Python team of developers, so I don’t know what they are thinking about or whether they have ever considered using a register-based interpreter. However, I do know that at Google there was a project called Unladen Swallow, which was a project to see if the Python interpreter could be reworked to perform faster. They ended up switching to a stack rather than a registered-base interpreter. Unfortunately, the unladen swallow project is discontinued, but a lot of its ideas did go into the PyPi imitation. In conclusion, this question has been thought of and it has underwent some discussion, but I think it is unlikely Python will go through an overhaul to adapt something entirely different.
Q: Why do You Have Such a Strong Opinion against a Following Feature on Stack Overflow?
Preface: On Meta Stack Exchange, Martijn argued against a following feature for Stack Overflow, his point being that Stack Overflow is not a social network and should never be a social network. However, to many beginners, Stack Overflow experts are much clearer and the documentation, so they would literally follow him and other experts with trillions of points, as they would get more out of scrolling through their answers and reading them. Thus, this viewer is curious why Martijn is so opposed to the following feature.
The following feature brings in the danger where people start to focus too much on a subset of experts, which is why I am so against the following feature. I was once new as well. Though my account says I’ve been on Stack Overflow for five years, I didn’t do anything at the very beginning until 2~2.5 years ago. Before that, I only answered a couple of clone questions here and there, so I’m actually relatively new to the scene. I haven been around as long as Jon Skeet, for example. Therefore, as an upcoming/newcoming guy, I wouldn’t want to have to buy my way into an established network of what people follow. Moreover, Stack Overflow, to me, is all about the content itself with the questions and answers, so I’d rather see a feed of all the questions and highly voted posts, because I’m not the only guy out there writing Python answers.
That said, you can get an RSS feed on all Stack Overflow answerers if you wanted to. There is an RSS icon on every page, where you can just scroll down on the bottom right-hand side and click on them to get the url for the feed. You can even follow all questions and tags. As I learned Python too long ago, I don’t know what sites to recommend for learning Python, but I’d probably follow Planet Python’s RSS feed if I were a beginner.
Other posts in this series with Martijn Pieters:
- Python Optimization and How it Can Affect Your Code
- 6 Useful Python Libraries Recommended by #1 Stack Overflow Answerer
- Python 2.7 vs Python 3.4 ─ What should Python Beginners choose?
- Martijn Pieters on the Future of Django
Need Martijn’s help? Book an 1-on-1 session!
Codementor is your live 1:1 expert mentor helping you in real time.