473,408 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

inheritance and private attributes

KN

I've run into such problem:

I have something like this:

class A(object):
def __init__(self):
self.__value = None

class B(A):
def test(self):
if self.__value:
print "Ok."
else:
print "Empty."
b = B()
b.test()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in test
AttributeError: 'B' object has no attribute '_B__value'

Why I have no access to private attribute from a class that
inherits other class? I just want to have access to the same
private variables but also to extend its functionality by
adding a method that operates on those private attributes and
I'm unable to do so.

Is this normal behaviour? What should I do if I want to
override method and use private attribute, or just add
some other method which changes this attribute?

/K

Jul 18 '05 #1
2 8073
KN wrote:

I've run into such problem:

I have something like this:

class A(object):
def __init__(self):
self.__value = None

class B(A):
def test(self):
if self.__value:
change the above to
if self._A__value:
print "Ok."
else:
print "Empty."
b = B()
b.test()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in test
AttributeError: 'B' object has no attribute '_B__value'

Why I have no access to private attribute from a class that
inherits other class? I just want to have access to the same
private variables but also to extend its functionality by
adding a method that operates on those private attributes and
I'm unable to do so.

Is this normal behaviour? What should I do if I want to
override method and use private attribute, or just add
some other method which changes this attribute?


Python performs name mangling for attributes starting with two underscores.
This is meant to avoid accidental nameclashes of attributes that are rather
an implementation detail than of interest to subclasses and the wider
public. I like many others use attribute names with a *single* leading
underscore (i. e. no name mangling) to signal "this is may change without
prior notice" while omitting artificial hurdles.
This is also known as treating programmers as adults :-)

Peter
Jul 18 '05 #2
KN <va**@dead.art.pl> wrote in message news:<ma**************************************@pyt hon.org>...
I've run into such problem:

I have something like this:

class A(object):
def __init__(self):
self.__value = None

class B(A):
def test(self):
if self.__value:
print "Ok."
else:
print "Empty."
b = B()
b.test() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in test
AttributeError: 'B' object has no attribute '_B__value'

Why I have no access to private attribute from a class that
inherits other class? I just want to have access to the same
private variables but also to extend its functionality by
adding a method that operates on those private attributes and
I'm unable to do so.

Is this normal behaviour? What should I do if I want to
override method and use private attribute, or just add
some other method which changes this attribute?

/K


This is normal behavior (by design). In general a derived class has
no special access to the attributes defined in its parent; there is no
equivalent of C++'s "protected" variables.

I see two solutions:

1) Have the child use an accessor function (if class A above had a
getValue() method, you could call it with A.getValue(self)).

2) Cheat. In class B, access self._A__value. This duplicates the
"mangling" that python does to hide variables with double underscore
in front.

I'd recommend 1), unless you have some urgent performance problem. 2)
is probably a bit faster.

Fast example of 2):
class a: .... def __init__(self):
.... a.val = 3
.... def getValue(self):
.... return a.__val
.... def __init__(self):
.... a.__val = 3 x = a()
a.getValue() 3 class b(a): .... def __init__(self):
.... a.__init__(self)
.... def getValue(self):
.... return a._a__val + 2
.... y = b()
y.getValue()

5
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
1
by: NotEnough | last post by:
I have a program assigment due next week. I have a a simple c++ grade average program below. How do I turn it into an inheritance. Where Grade is the base class. And Quiztest - Project - LabTest -...
0
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
8
by: Sam Kuehn | last post by:
What is the correct way to "widen" the interface of an object? Let me explain (this is not a real example but should illustrate my point). Say you have a person object with some properties. And...
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
8
by: Brian Munroe | last post by:
My example: class A(object): def __init__(self, name): self.__name = name def getName(self): return self.__name
5
by: Rafe | last post by:
Hi, I've been thinking in circles about these aspects of Pythonic design and I'm curious what everyone else is doing and thinks. There are 3 issues here: 1) 'Declaring' attributes - I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.