Don’t Object to Python Objects

Don’t Object to Python Objects

There’s the old joke about 10 kinds of programmers, but the truth is when it comes to programming, there are often people who make tools and people who use tools. The Arduino system is a good example of this. Most people use it like a C compiler. However, it really uses C++, and if you want to provide “things” to the tool users, you need to create objects. For example, when you put Serial in a program, you use an object someone else wrote. Python — and things like Micropython — have the same kind of division. Python started as a scripting language, but it has added object features, allowing a rich set of tools for scripters to use. [Damilola Oladele] shows the ins and outs of object-oriented Python in a recent post.


Like other languages, Python allows you to organize functions and data into classes and then create instances that belong to that class. Class hierarchies are handy for reusing code, customizing behavior, and — through polymorphism — building device driver-like architectures.



For example, you might build a class for temperature sensors and then create specialized subclasses for different specific sensors. The code to convert the sensor reading to degrees would live in each subclass. However, common code, such as getting an average of several samples, could be used in the main class. Even more importantly, any part of your code that needs a temperature sensor will just deal with the main class and won’t care what kind of sensor is actually in use except, of course, when you instantiate the sensor.


Python’s implementation of object orientation does have a few quirks. For example, if you create a class variable, it can be read from a ..

Support the originator by clicking the read the rest link below.