- Published: May 30, 2016
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.
- 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.
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 environment isolation with pip and virtualenv is recommended, so each project can handle their own dependencies and language versions.
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.
A small ORM that supports postgresql, mysql and sqlite. Can be used with models like Django.
*[ORM]: Object Relational Mapping