473,698 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derived / base class name conflicts

Suppose you want to write a subclass of some existing class you are
importing from a module you didn't write and that you don't want to
study the internals of, and you want to define a data member i in your
constructor.

As in the following:

from module1 import A

class B(A):
def __init__(self):
A.__init__(self )
self.i= 0
b= B()

Now, 'i' might have already been defined by A or by the call to
A.__init__() so if you define it without knowing that, you could be
changing the behavior of A's methods in unknown ways, which is
obviously a bad thing.

One way to avoid this is to run the following program to clear the name
'i' first:

from module1 import A
a= A()
print a.i

If you get an AttributeError, you know the name 'i' is safe to use. If
you actually get some sort of report from the print statement, then you
will know that 'i' is not safe to use.

This strikes me as a rather odd procedure to go through, but I don't
see any way around it.

It there some other way to handle this issue?

Do I actually need to study the sources for / implementation of
Tkinter.Canvas before I can safely subclass it (and assign attributes
to an instance of the subclass) or clear attribute names as I outlined
above?

Chris Marshall

Nov 10 '05
12 1784
ch************* *****@yahoo.com a écrit :
Your suggestion ('_name' -> implementation, 'name' -> API)
This is not "my" convention, it's *the* (mostly agreed upon) Python
convention. Like 'self', or CONSTANT, or a whole lot of things in Python.
makes sense
as a convention between programmers that know a fair amount about each
other's classes before using them.
No need to know much. dir(modulename. ClassName) is enough.
I don't think it is reasonable in general to only subclass from base
classes you have studied the full API of, however.
Depends on your definition of "studying the full API" !-)

Now if your fear is to accidentally override something in the base
class, it's just a matter of:
print "API:"
print [name for name in dir(modulename. ClassName) \
if not name.startswith ('_')]

print "IMPLEMENTATION :"
print [name for name in dir(modulename. ClassName) \
if not name.startswith ('_')]
The double
underscore is a decent solution to my problem.
Possibly. It can also become a PITA. But it's you who know what your
code need !-)
I imagine it must be used a lot in domains where people are making
heavy use of third party python code.


I almost never used it (perhaps half-a-dozen times, and only in
frameworks abstract base classes), and rarely saw it in 3rd part source
code.
Nov 22 '05 #11
ch************* *****@yahoo.com wrote:
so the following would not result in any conflicts

class A:
def __init__(self):
self.__i= 0

class B(A):
def __init__(self):
A.__init__(self )
self.__i= 1


Be careful here. The above won't result in any conflicts, but related
cases, where you have two classes with the same name in different
modules, may still result in conflicts. See my previous posts on this:

http://groups.google.com/group/comp....3984abaee1c2b5
http://groups.google.com/group/comp....3183a2c01c8ecf

However, I tend not to use double-underscore name mangling, and I don't
think I've ever had a shadowing problem, so clearly I wouldn't have
shadowing problems with double-underscore name mangling either...

STeVe
Nov 22 '05 #12
ch************* *****@yahoo.com wrote:
so the following would not result in any conflicts

class A:
def __init__(self):
self.__i= 0

class B(A):
def __init__(self):
A.__init__(self )
self.__i= 1


Be careful here. The above won't result in any conflicts, but related
cases, where you have two classes with the same name in different
modules, may still result in conflicts. See my previous posts on this:

http://groups.google.com/group/comp....3984abaee1c2b5
http://groups.google.com/group/comp....3183a2c01c8ecf

However, I tend not to use double-underscore name mangling, and I don't
think I've ever had a shadowing problem, so clearly I wouldn't have
shadowing problems with double-underscore name mangling either...

STeVe
Nov 22 '05 #13

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

Similar topics

14
3325
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this instance!
24
3296
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
6
6659
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
6621
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
6
6054
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The problem is that in my derived classes, the XML that is automatically generated is lacking the properties of the base class. For example: public class MyBaseClass { public MyBaseClass ( ) { } private string myVariable;
1
831
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead of time for any help or feedback. Cheers, Peter
6
2496
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
15
2150
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to have an attribute which can be applied to a class definition of a class which inherits from a base, mustinherit class. I want to define methods in the base class which will access the contents of the attribute as it is applied to
3
2223
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling the subroutine. I know the base class name can be retrieved by using reflection namespace: System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name. Thanks Goran
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7708
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.