Phyton programming language

Object Oriented concepts in Python

The rule of thumb is, don’t introduce a new attribute outside of the init method, otherwise you’ve given the caller an object that isn’t fully initialized. – https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/

  • Why you need self as the first positional argument of a method

In Python, this: my_object.method("foo") is syntactic sugar, which the interpreter translates behind the scenes into: MyClass.method(my_object, "foo") which, as you can see, does indeed have two arguments - it’s just that the first one is implicit, from the point of view of the caller. This is because most methods do some work with the object they’re called on, so there needs to be some way for that object to be referred to inside the method. By convention, this first argument is called self inside the method definition.

Environment isolation

Same database engine in all the environments

The same database engine (e.g. PostgreSQL, sqlite, MySQL), should be used in all the different environments of a project.

It is a common bad practice to have a lightweight database in the development environment and something more powerful in production, all of them should use the same engine to avoid the different ways each one handle the data.

This way it is possible to:

  • examine the production environment data locally.
  • avoid the different types and constraints each database engine has. (e.g. sqlite does not support foreign key constraint, while PostgreSQL does )

Project packages

Project environment isolation with pip and virtualenv is recommended, so each project can handle their own dependencies and language versions.

OS isolation

If there is more than one computer in the development process, there would be probably different Operating Systems and software versions involved that can lead to inconsistencies in source code. There are two applications that play nice together:

  • Vagrant creates reproducible development environments. In addition to being useful in setting up developers environments, it makes easier to configure development, staging and production environment.

  • VirtualBox is a virtualization product, makes it easy to have the same OS that the Vagrant file is built for.

Project structure

Database

Peewee

A small ORM that supports postgresql, mysql and sqlite. Can be used with models like Django.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.