Python Forum
Can access class private variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can access class private variable?
#1
Hi,

Am new to Python3 (previously Java)
Below I make a class, instantiate it, and then do 2 things I don't understand (see comments)

Appreciate some help - Michael


class HWClass:

    def __init__(self): 
        self.__classvar = 3 #private
        self.printHW()

    def printHW(self): 
        print("Hello World")

hw = HWClass()  #Instantiation
hw.__classvar = 4  #This suggests I change a private class variable? It doesn't actually change, but the text suggests it does, which is error-prone
hw.varxxxx = 9 #How can I use a variable on this class object that is not defined in the class?
Reply
#2
Actually, hw.__classvar does change. Python does not have private variables. There is name mangling, but that's really not worth messing with. There's also properties, but that's more about setters/getters than private variables.

Also, classes accept new variables in their namespace. That's just how Python works. You can limit this behavior with __slots__, but that is rarely, if ever, done.

Python is more about making it easy to do things than it is about making rules that you can't do things.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You can access it bye hw._HWClass__classvar  not hw.__classvar.
>>> hw = HWClass()
>>> hw._HWClass__classvar
3
>>> hw._HWClass__classvar = 4
>>> hw._HWClass__classvar
4
As mention there is nothing private in Python.
__ would say it should not be used before understanding what's it's used for(name mangling).
A quick demo of name mangling.
class HWClass:
    def __init__(self):
        self.__classvar = 3 # Not private, it's for name mangling

class HW(HWClass):
    def __init__(self):
        super().__init__()
        self.__classvar = 3 
Now if use Class HW,
has class variable classvar been named mangled(not overwritten).
>>> hw = HW()
# Both have been given different name(name mangling)
>>> hw._HWClass__classvar
3
>>> hw._HW__classvar
3

# Change one will not change the other
>>> hw._HWClass__classvar = 100
>>> hw._HWClass__classvar
100
>>> hw._HW__classvar
3
It's take some chaining to learn it the Python way,when coming from Java.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I access objects or widgets from one class in another class? Konstantin23 3 999 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Lint and private var names PatM 1 686 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  Cant access variable from anywhere Frankduc 33 4,521 Nov-09-2022, 03:33 PM
Last Post: deanhystad
  Variable Types in a class nafshar 9 2,452 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,888 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,311 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,629 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,683 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Access instance of a class Pavel_47 5 2,085 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,058 Nov-18-2021, 03:02 PM
Last Post: drSlump

Forum Jump:

User Panel Messages

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