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


Messages In This Thread
Can access class private variable? - by Michael - Aug-11-2017, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I access objects or widgets from one class in another class? Konstantin23 3 1,047 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Lint and private var names PatM 1 714 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  Cant access variable from anywhere Frankduc 33 4,638 Nov-09-2022, 03:33 PM
Last Post: deanhystad
  Variable Types in a class nafshar 9 2,519 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,984 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,344 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,666 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,720 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Access instance of a class Pavel_47 5 2,118 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,077 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