Skip to content
Advertisement

How do I create an alias for a variable in Python?

Normal way:

JavaScript

Aliased approach:

JavaScript

How does one accomplish aliasing in Python? The reason I want to do this is to reduce cluttering due to long variable names. It’s a multi threaded environment, so simply copying to a local variable will not work.

Advertisement

Answer

The solution to this is to use getter and setter methods – fortunately Python has the property() builtin to hide the ugliness of this:

JavaScript

Generally, deeply nested attributes like self.a.b.c are a sign of bad design – you generally don’t want classes to have to know about objects that are 3 relationships away – it means that changes to a given item can cause problems throughout your code base. It’s a better idea to try and make each class deal with the classes around it, and no further.

Advertisement