58

How do I access a private attribute of a parent class from a subclass (without making it public)?

unwind
402k64 gold badges492 silver badges620 bronze badges
asked Apr 28, 2009 at 12:55
2
  • 7
    Do you mean protected, or private? Your question is different from your title ... Commented Apr 28, 2009 at 13:00
  • 13
    Since protected and private don't mean much in Python, please provide actual code. Commented Apr 28, 2009 at 13:10

9 Answers 9

109

My understanding of Python convention is

  • _member is protected
  • __member is private

Options for if you control the parent class

  • Make it protected instead of private since that seems like what you really want
  • Use a getter (@property def _protected_access_to_member...) to limit the protected access

If you don't control it

  • Undo the name mangling. If you dir(object) you will see names something like _Class__member which is what Python does to leading __ to "make it private". There isn't truly private in python. This is probably considered evil.
community wiki

Sign up to request clarification or add additional context in comments.

1 Comment

Important note: the purpose of the __ prefix is not to make the attribute private in a visibility sense but in an inheritance sense, mostly in order to avoid naming conflicts when a class is designed specifically for inheritance. As the comment notes, the mangling scheme is well defined and stable, so it won't stop anyone from accessing the attribute (or updating it) if they really want to.
67

Two philosophies of protection

Some language designers subscribe to the following assumption:

"Many programmers are irresponsible, dim-witted, or both."

These language designers will feel tempted to protect programmers from each other by introducing a private specifier into their language. Shortly later they recognize that this is often too inflexible and introduce protected as well.

Language designers such as Python's Guido van Rossum, in contrast, assume that programmers are responsible adults and capable of good judgment (perhaps not always, but typically). They find that everybody should be able to access the elements of a program if there is a need to do that, so that the language does not get in the way of doing the right thing. (The only programming language that can reliably get in the way of doing the wrong thing is the NULL language)

Therefore, _myfield in Python means something like "The designer of this module is doing some non-obvious stuff with this attribute, so please do not modify it and stay away from even reading it if you can -- suitable ways to access relevant information have been provided (or so we hope)."

In case you are not able to stay away from accessing _myfield (such as in special cases in a subclass), you simply access it.

answered Jan 19, 2014 at 13:06

1 Comment

Can you believe it @san, the analogy has already been made?
19

Using @property and @name.setter to do what you want

e.g

class Stock(object):
 def __init__(self, stockName):
 # '_' is just a convention and does nothing
 self.__stockName = stockName # private now
 @property # when you do Stock.name, it will call this function
 def name(self):
 return self.__stockName
 @name.setter # when you do Stock.name = x, it will call this function
 def name(self, name):
 self.__stockName = name
if __name__ == "__main__":
 myStock = Stock("stock111")
 myStock.__stockName # It is private. You can't access it.
 #Now you can myStock.name
 N = float(raw_input("input to your stock: " + str(myStock.name)+" ? "))
answered Apr 15, 2011 at 21:39

2 Comments

The question was about accessing the attribute in a subclass.
I modified your example a little bit. See below.
14

No one seems to answer the original question

how to access a private attribute of a parent class from a subclass

So here's a simple use case, demonstrating two options - accessing parent class __private variables and using @property decorator:

class Family:
 def __init__(self, name):
 self.__family_name = name
 @property
 def name(self):
 return self.__family_name
class Child(Family):
 def __init__(self, first_name, last_name):
 super(Child, self).__init__(last_name)
 self.__child_name = first_name
 @property
 def name(self):
 return (self.__child_name, super(Child, self).name)
if __name__ == '__main__':
 my_child = Child("Albert", "Einstein")
 # print (my_child.__child_name) # AttributeError - trying to access private attribute '__child_name'
 # print (my_child.__family_name) # AttributeError - trying to access private attribute '__family_name'
 print (my_child._Child__child_name) # Prints "Albert" - By accessing __child_name of Child sub-class
 print (my_child._Family__family_name) # Prints "Einstein" - By accessing __family_name in Family super-class
 print (" ".join(my_child.name)) # Prints "Albert Einstein" - By using @property decorators in Child and Family 
answered Aug 27, 2017 at 11:31

1 Comment

Thanks for answering the actual question instead of using the question as a pulpit. For anyone else landing here, the bit in the docs that explains this is @ docs.python.org/3/tutorial/classes.html#private-variables, specifically the "name mangling" bit. Noam's answer explained it well, but I dug out the docs cos I wanted to read the official wording on it too. Cheers.
4

if the variable name is "__secret" and the class name is "MyClass" you can access it like this on an instance named "var"

var._MyClass__secret

The convention to suggest/emulate protection is to name it with a leading underscore: self._protected_variable = 10

Of course, anybody can modify it if it really wants.

answered Apr 28, 2009 at 13:00

Comments

2

I think this code is a little clearer than Steve's. Steve's answer was most helpful for what I am trying to do, so thank you! I tested this with python 2.7 and python 3.6.

#! /usr/bin/python
#
# From https://stackoverflow.com/questions/797771/python-protected-attributes
from __future__ import print_function
import sys
class Stock(object):
 def __init__(self, stockName):
 # '_' is just a convention and does nothing
 self.__stockName = stockName # private now
 @property # when you do Stock.name, it will call this function
 def name(self):
 print("In the getter, __stockName is %s" % self.__stockName, file=sys.stderr)
 return self.__stockName
 @name.setter # when you do Stock.name = x, it will call this function
 def name(self, name):
 print("In the setter, name is %s will become %s" % ( self.__stockName, name), file=sys.stderr)
 self.__stockName = name
if __name__ == "__main__":
 myStock = Stock("stock111")
 try:
 myStock.__stockName # It is private. You can't access it.
 except AttributeError as a:
 print("As expect, raised AttributeError", str(a), file=sys.stderr )
 else:
 print("myStock.__stockName did did *not* raise an AttributeError exception")
 #Now you can myStock.name
 myStock.name = "Murphy"
 N = float(input("input to your stock: " + str(myStock.name)+" ? "))
 print("The value of %s is %s" % (myStock.name, N) )
answered Oct 22, 2017 at 5:51

Comments

0

Make an accessor method, unless I am missing something:

def get_private_attrib(self):
 return self.__privateWhatever
answered Apr 28, 2009 at 12:57

Comments

0

Code modified from geeksforgeeks

# program to illustrate protected
# data members in a class 
 
 
# Defining a class
class Geek: 
 # private
 __name = "R2J"
 # protected data members 
 _roll = 1706256
 def __init__(self, value):
 value1 = value
 self.value2 = value
 # public member function 
 def displayNameAndRoll(self): 
 
 # accessing protected data members 
 print("Name: ", self.__name) 
 print("Roll: ", self._roll) 
 
 
# creating objects of the class 
obj = Geek(1)
obj.__name = 'abc'
obj._roll = 12345
# calling public member 
# functions of the class 
obj.displayNameAndRoll() 

Note that I can modifies without setter the protected attribute, but not the private.

Name: R2J
Roll: 12345

OBS: running in python 3.8

answered Feb 7, 2022 at 17:51

Comments

0

There is a process of name mangling when you instantiate a non public attribute. In order to access the attribute outside of the class you need to "un-mangle".

Example: Class creation

class Object:
 def __init__(self, name):
 self.__name = name

Create instance:

obj = Object('test')

Access non public attribute (un-mangle):

print(obj._Object__name)
answered Jun 8, 2022 at 12:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.