473,945 Members | 25,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you make issubclass work

Okay I have two files:

a.py:
-----
class cla_a(object): pass

class cla_c(object): pass

if __name__ == "__main__":
mod = __import__("b")
attr = getattr(mod, "cla_b")
print issubclass(attr , cla_a)
print issubclass(cla_ c, cla_a)
-----

and b.py:
-----
from a import cla_a

class cla_b(cla_a): pass
-----

now if I do 'python a.py'
it gives me:
False
True

Why is cla_b a subclass and not cla_c?

I think that it has something to with the fact that cla_b is a
subclass of a different instance of cla_a or something like that. But
if that is the case how do I do this sort of thing in python?

Nathan Bullock

http://bullock.mooo.com
Jul 18 '05 #1
4 3441
Nathan Bullock wrote:
Okay I have two files:

a.py:
-----
class cla_a(object): pass

class cla_c(object): pass

if __name__ == "__main__":
mod = __import__("b") # why not
import b as mod
attr = getattr(mod, "cla_b")
# why not
attr = mod.cla_b
print issubclass(attr , cla_a)
print issubclass(cla_ c, cla_a)
-----

and b.py:
-----
from a import cla_a

class cla_b(cla_a): pass
-----

now if I do 'python a.py'
it gives me:
False
True
Should be:
True
False
Why is cla_b a subclass and not cla_c?


Here you have it right again.

That you have two different modules does not affect the class hierarchy.
Y is a subclass of X if X can be found in the inheritance tree of Y, i. e.
occurs directly in Y's bases
class X: pass .... class Y(X): pass .... issubclass(Y, X) True

or indirectly in the bases' bases:
class X: pass .... class Intermediate(X) : pass .... class Y(Intermediate) : pass .... issubclass(Y, X) True

On the other hand it is not sufficient for a subclass relationship when two
classes share a common ancestor:
class A: pass .... class B(A): pass .... class C(A): pass .... issubclass(B, C), issubclass(C, B)

(False, False)

So you can think of subclasses as children and grandchildren, but not as
cousins.

[As modules are cached, subsequent imports of the same module yield the same
module instance. Therefore the same classes (cla_a is cla_a == True) are
seen by all parts of a program and it (normally) doesn't matter in what
module a class is defined.]

Peter

Jul 18 '05 #2
I think my first post was a little unclear. I will try again.

I have two files, the first is this:

File b.py:
-------------------
from a import cla_a

class cla_b(cla_a): pass
-------------------
File a.py
-------------------
class cla_a(object): pass

class cla_c(cla_a): pass

if __name__ == "__main__":
import b as mod
print issubclass(mod. cla_b, cla_a)
print issubclass(mod. cla_b, mod.cla_a)
print issubclass(cla_ c, cla_a)
print mod.cla_a is cla_a
-------------------

Results of 'python a.py'

False
True
True
False
-----------------------------

This is using python 2.3.3. Is this a bug? Why do we have two
different cla_a's which are not the same? And do to this problem how
in the world do I determine if a cla_b is a subclass of cla_a?
Obviously they are, but issubclass doesn't realize it because the
cla_a that I am testing with is not the same instance of cla_a as
cla_b was derived from.

[As modules are cached, subsequent imports of the same module yield the same
module instance. Therefore the same classes (cla_a is cla_a == True) are
seen by all parts of a program and it (normally) doesn't matter in what
module a class is defined.]

Peter


Now I take it from this answer from Peter, that if I did the test in a
third file so that I was using an imported instance of cla_a that
issubclass would then work properly.

Nathan
Jul 18 '05 #3
Nathan Bullock wrote:
This is using python 2.3.3. Is this a bug? Why do we have two
different cla_a's which are not the same?
Your file 'a.py' is two things. It's the code being used
as the "__main__" module *and* it's used as the "a" module.

Add this to your a.py:__main__ to see the difference.

import sys
main_module = sys.modules["__main__"]
print "main is from", main_module.__f ile__
a_module = sys.modules["a"]
print "a is from", a_module.__file __
print "Are the modules the same?", a_module == main_module
print "Are the classes the same?", a_module.cla_a ==
main_module.cla _a

You'll see that the file a.py is used twice, and the
class cla_a defined once as __main__.cla_a and the other
as a.cla_a .

To make what you want work, well, a good rule is to avoid
circular imports. Another is that your main code should
not be imported. But if you want so see your code to work
as you expect it to work, you need to change b.py so that
the "from a import cla_a" is instead
"from __main__ import cla_a"

Now I take it from this answer from Peter, that if I did the test in a
third file so that I was using an imported instance of cla_a that
issubclass would then work properly.


It should.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #4
Andrew Dalke <ad****@mindspr ing.com> wrote:
...
To make what you want work, well, a good rule is to avoid
circular imports. Another is that your main code should
not be imported. But if you want so see your code to work
as you expect it to work, you need to change b.py so that
the "from a import cla_a" is instead
"from __main__ import cla_a"


I would suggest a different workaround if it's important that module
a.py be runnable as main _and_ importable as a without getting two
copies: start a.py with the incantation...

import sys
if __name__!='a': sys.modules[a]=sys.modules[__name__]
After this, if any other module should 'import a', it WILL get the
module from a.py, not a separate copy. This won't solve everything, by
a long shot (circular imports _are_ trouble, period), but it will at
least remove the single issue of the "two modules from the same a.py
source file"...!
Alex
Jul 18 '05 #5

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

Similar topics

7
4875
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does not exist... What can I do about this and way does it not work in win 98?
1
2191
by: Chris Green | last post by:
Heyas folks, When does issubclass fail? That's a loaded question so here's my test case (also available at http://cmg.dok.org/code/classimports.tar.gz): directory structure: ../test.py ../d/__init__.py ../d/b.py
3
3064
by: Douglas Buchanan | last post by:
Buttons don't work if form is opened on startup A2k If 'frmMain' is set to open by default at startup none of the buttons work. If 'frmMain' is opened from the database window then all the buttons work. The form's name ('frmMain') is selected from in the Startup dialog box.
4
2093
by: Paul T. RONG | last post by:
Dear All, I add two new tables to the database and then the disable shift key codes don't work. These two new tables are actually created by two queries, and only these two are in the front end, I didn't manage to move them to the back end. I work with Access 2k under Win XP. these are the codes that don't work (it worked for more than a year very well):
15
1928
by: Brett | last post by:
I'd like to know what management and the work environment are like where you work. I need something relative to compare my current work environment. Here's a few questions I have: 1.) Is it normal for (technical) management (actually 1 of 2 co owners) to constantly interrupt developers all day with with questions such as: "can we look at this", "I found this, can we try it now", "Can we see what these guys are doing", "can we change...
2
7733
by: Holysmoke | last post by:
Hi, I have a asp.net form with a button in it. In the onlclick event of button I want to write a function which does some a bit of work with a timespan of 15 seconds. So I want to show a image something like "Work In Progress" when this process is going on and hide it when the work done I do something like this button click event
33
2027
by: bonk | last post by:
I have an application that needs to perform some background work, i.e. Logging, wich must not block the main thread. How would I basically design such a scenario? It is obvious that I should do that on an extra thread but I think it is a bad idea to spawn a new thread everytime a log-message is written and let it die once the message was written. But how do I keep a thread alive and trigger the log-messages and pass the string to that...
11
53159
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/information_overloaded.jpgPaul Graham wrote an interesting article a few months back about how the internet is leading to information overload for information workers of today. He is not alone in his thinking. Similarly, In July of 2008 IBM, Intel, Microsoft and Xerox announced that they were joining forces with Information Overload Research Group(http://www.iorgforum.org/) to combat one of the greatest...
0
10149
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11140
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...
0
7402
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
6093
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
6315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4927
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3523
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.