Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`final` keyword equivalent for variables in Python?

I couldn't find documentation on an equivalent of Java's final in Python, is there such a thing?

I'm creating a snapshot of an object (used for restoration if anything fails); once this backup variable is assigned, it should not be modified -- a final-like feature in Python would be nice for this.

like image 862
Jason Coon Avatar asked Apr 29 '09 14:04

Jason Coon


People also ask

What is a final variable in Python?

Definition and Usage The finally keyword is used in try... except blocks. It defines a block of code to run when the try... except...else block is final. The finally block will be executed no matter if the try block raises an error or not.

What does the final keyword do to a variable?

What is the Final Keyword in Java? Java final keyword is a non-access specifier that is used to restrict a class, variable, and method. If we initialize a variable with the final keyword, then we cannot modify its value. If we declare a method as final, then it cannot be overridden by any subclasses.

Can we declare variable as final?

If a variable is declared with the final keyword, its value cannot be changed once initialized. Note that the variable does not necessarily have to be initialized at the time of declaration. If it's declared but not yet initialized, it's called a blank final variable.

What is known as final variable?

A final variable is called a blank final variable if it is not initialized while declaration. Below are the two ways to initialize a blank final variable. A blank final variable can be initialized inside an instance-initializer block or inside the constructor.


2 Answers

Having a variable in Java be final basically means that once you assign to a variable, you may not reassign that variable to point to another object. It actually doesn't mean that the object can't be modified. For example, the following Java code works perfectly well:

public final List<String> messages = new LinkedList<String>();  public void addMessage() {     messages.add("Hello World!");  // this mutates the messages list } 

but the following wouldn't even compile:

public final List<String> messages = new LinkedList<String>();  public void changeMessages() {     messages = new ArrayList<String>();  // can't change a final variable } 

So your question is about whether final exists in Python. It does not.

However, Python does have immutable data structures. For example, while you can mutate a list, you can't mutate a tuple. You can mutate a set but not a frozenset, etc.

My advice would be to just not worry about enforcing non-mutation at the language level and simply concentrate on making sure that you don't write any code which mutates these objects after they're assigned.

like image 122
Eli Courtwright Avatar answered Sep 22 '22 09:09

Eli Courtwright


There is no final equivalent in Python. 

But, to create read-only fields of class instances, you can use the property function.

Edit: perhaps you want something like this:

class WriteOnceReadWhenever:     def __setattr__(self, attr, value):         if hasattr(self, attr):             raise Exception("Attempting to alter read-only value")          self.__dict__[attr] = value 
like image 37
Stephan202 Avatar answered Sep 21 '22 09:09

Stephan202