Python Forum
Inheritance private attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance private attributes
#1
Hello guys,

I'm trying to inherit private attributes (self.__attrib), but when I use the parent __init__ method this private attributes doesn't inherit. For example:

class Primary():
	def __init__(self, param1, param2):
		self.param1 = param1
		self.__param2 = param2

	def getP1(self):
		return self.param1

	def getP2(self):
		return self.__param2

class Secondary(Primary):
	def __init__(self, param1, param2, param3):
		Primary.__init__(self, param1, param2)
		self.__param3 = param3

	def getP3(self):
		return self.__param3

	def test(self):
		print (self.__param2)

#Main

p = Primary('a', 'b')
s = Secondary('a', 'b', 'c')

s.test()
When I try to access self.__param2 in Secondary instance, there is an error:
Error:
Traceback (most recent call last): File "class_test.py", line 38, in <module> s.test() File "class_test.py", line 21, in test print (self.__param2) AttributeError: 'Secondary' object has no attribute '_Secondary__param2'
I think in three solutions:
1 - Use public attributes: self.param2 instead of self.__param2. It is not an important problem, but conceptually, the attributes that will not be accessed out of class, must be private. Not?

2- Not reutilize the Primary __init__ constructor. But we are wasting inheritance potencial, not? Like this:

class Secondary(Primary):
	def __init__(self, param1, param2, param3):
		self.param1 = param1
		self.__param2 = param2
		self.__param3 = param3
3- Use parent get() method when I need it (maybe the best solution):
def test(self):
		print (Primary.getP2(self))
Any best solution? Maybe the best is the third option, not?

Thanks a lot Big Grin
Reply
#2
class Primary():
    def __init__(self, param1, param2):
        self.param1 = param1
        self.__param2 = param2

    def getP1(self):
        return self.param1

    def getP2(self):
        return self.__param2

class Secondary(Primary):
    def __init__(self, param1, param2, param3):
        Primary.__init__(self, param1, param2)
        self.__param3 = param3

    def getP3(self):
        return self.__param3

    def test(self):
        print (self.getP2())

#Main

p = Primary('a', 'b')
s = Secondary('a', 'b', 'c')

s.test()
Reply
#3
Thanks Larz60+

By the way ... I do not see many examples of private attributes in Python. It seems that they get used to using public attributes. I have been years without programming, but in C, C ++ ... as a rule I always used private attributes.

Something similar I find with abstract classes. In Python it is used rather little.

Is there any reason in this regard?

Thanks!
Reply
#4
There are no reglementations concerning private attributes. There is only a coding convention (like you used in your code) to mark private attributes but in general they behave like global variables. It's a bit different in python in comparison to C/C++ or Java. Somehow the same goes for abstract classes. There are abstract classes and also I like to program with these since they are a good programming technique. But also those methods only can be marked as abstract and can vary in its given attributes so that a method in a child class only needs the same name. Both concepts exist in python and are used, but they work in a different way than you may know it from other programming languages :)
Reply
#5
(May-02-2018, 08:18 PM)vaison Wrote: the attributes that will not be accessed out of class, must be private. Not?
There is nothing private in Python,and getter/setter should not be used.
__ is for name mangling and is not private.
A quick demo of name mangling.
class Foo:
    def __init__(self):
        self.__var = 3 # Not private, it's for name mangling

class Bar(Foo):
    def __init__(self):
        super().__init__()
        self.__var = 3
Test usage:
>>> obj = Bar()
>>> # Both have been given different name(name mangling)
>>> obj._Foo__var
3
>>> obj._Bar__var
3
>>> 
>>> # Change one will not change the other
>>> obj._Foo__var = 100
>>> obj._Foo__var
100
>>> obj._Bar__var
3
Reply
#6
Hello,

ok, thanks for the aclarations. I have it much clearer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Lint and private var names PatM 1 684 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  Unable to import Private Repo using setup.py Bob786 1 1,738 Sep-02-2021, 04:19 PM
Last Post: snippsat
  python 3 dns lookup private domain didact 1 2,549 Sep-19-2020, 06:01 PM
Last Post: bowlofred
  [split] Помощь по приватным ключам/Private key help sairam17519 0 1,602 Sep-07-2020, 12:55 PM
Last Post: sairam17519
  Download file from Private GitHub rep vinuvt 0 1,964 Jul-27-2020, 11:38 AM
Last Post: vinuvt
  Private package distribution abomination disadvantages research andreir 2 2,156 May-07-2020, 12:32 AM
Last Post: andreir
  Помощь по приватным ключам/Private key help vlrubl777 5 5,901 Mar-15-2019, 08:16 PM
Last Post: vlrubl777
  when to make attributes private? sneakyimp 10 5,868 Jan-21-2019, 02:54 PM
Last Post: sneakyimp
  Fetching private ip address from instances of an autoscaling group deepsonune 0 3,264 May-18-2018, 10:32 AM
Last Post: deepsonune
  Class Attributes Inheritance Harry_Potter 3 3,826 Nov-16-2017, 07:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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