Python Forum
Unchangeable variables in a class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unchangeable variables in a class?
#1
I want a class that will accept a value, and provide some other values based on calculations from the original value. I do not want these calculated values to be changeable.

Sample code (Python 2.7):
class MyNum:
    def __init__(self, value):
        self.value = value
        self.double = self.value * 2

x = MyNum(2)
print(x.value, x.double)
x.double = 8
print(x.value, x.double)
Output is:
(2, 4)
(2, 8)

I would like to see an error when I try to assign x.double a value. How can I prevent my class variable from being changed?
Reply
#2
From what I find online, the following code SHOULD do what I want, but it still isn't working as expected:

class MyNum:
    
    def __init__(self, value):
        self._value = value
        self._double = value * 2
        
    @property
    def value(self):
        return self._value
    
    @property
    def double(self):
        return self._double
    
x = MyNum(2)
print(x.value, x.double)
x.double = 8
print(x.value, x.double)
The output is still:
(2, 4)
(2, 8)
Reply
#3
You must be running some other code. My output for your code is
Error:
λ python paillasse/pf/calab.py 2 4 Traceback (most recent call last): File "/home/eric/Projets/Scratch/2023-01/paillasse/pf/calab.py", line 17, in <module> x.double = 8 AttributeError: can't set attribute 'double'
as it should be.

I hope you are not running Python 2, are you?
Reply
#4
Still changes
class MyNum:
     
    def __init__(self, value):
        self._value = value
        self._double = value * 2
         
    @property
    def value(self):
        return self._value
     
    @property
    def double(self):
        return self._double
     
x = MyNum(2)
print(x.value, x.double)
x._double = 8
print(x.value, x.double)
Output:
2 4 2 8
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Sep-15-2023, 03:20 PM)menator01 Wrote: Still changes
How are you using Python, which version, which setup? There must be some mistake.
Reply
#6
Version 3.10 on ubuntu with vscode editor
I've been doing a little reading on this, from what I understand that there is no way to prevent either class or instance variables from being changed.
The closest thing I found was some thing about using __slots__
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
(Sep-15-2023, 03:02 PM)Gribouillis Wrote: You must be running some other code. My output for your code is
Error:
λ python paillasse/pf/calab.py 2 4 Traceback (most recent call last): File "/home/eric/Projets/Scratch/2023-01/paillasse/pf/calab.py", line 17, in <module> x.double = 8 AttributeError: can't set attribute 'double'
as it should be.

I hope you are not running Python 2, are you?
I am running Python 2.7.13, only because the system we are working on needs to support a number of old python scripts.

Your output is what I would expect to get, but that's not occurring here. I am running this in a Jupyter notebook, but that shouldn't impact the behaviour here.
Reply
#8
(Sep-15-2023, 03:45 PM)menator01 Wrote: Version 3.10 on ubuntu
I can't believe it. Can you add
import sys
print(sys.version)
Reply
#9
import sys
print(sys.version)

class MyNum:
      
    def __init__(self, value):
        self._value = value
        self._double = value * 2
          
    @property
    def value(self):
        return self._value
      
    @property
    def double(self):
        return self._double
      
x = MyNum(2)
print(x.value, x.double)
x._double = 8
print(x.value, x.double)
Output:
3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] 2 4 2 8
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#10
(Sep-15-2023, 03:48 PM)Calab Wrote: I am running Python 2.7.13
You shouldn't run Python 2.7. However a solution to your problem is to write
class MyNum(object):
    ...
In Python 2, classes that don't subclass object explicitly are "old style classes", they actually belong to Python 1 and don't behave the same way. We're back more than 20 years ago with your code.

@menator01 The problem is that you're using x._double = 8 instead of x.double = 8. You are breaking the encapsulation.
Calab likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,903 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 10,784 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,905 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Class variables menator01 2 2,020 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,024 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Understanding Class Variables vindo 9 4,066 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 3,021 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Base class variables are not accessible Prabakaran141 3 2,830 Oct-31-2018, 07:13 AM
Last Post: buran
  Class Modules, and Passing Variables: Seeking Advice Robo_Pi 21 10,302 Mar-02-2018, 05:22 PM
Last Post: snippsat
  Running Class methods in a loop and updating variables. ujjwalrathod007 3 6,393 Oct-05-2016, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020