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


Messages In This Thread
Inheritance private attributes - by vaison - May-02-2018, 08:18 PM
RE: Inheritance private attributes - by Larz60+ - May-02-2018, 08:29 PM
RE: Inheritance private attributes - by vaison - May-02-2018, 08:56 PM
RE: Inheritance private attributes - by ThiefOfTime - May-02-2018, 09:04 PM
RE: Inheritance private attributes - by snippsat - May-02-2018, 09:06 PM
RE: Inheritance private attributes - by vaison - May-03-2018, 09:22 AM

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