473,379 Members | 1,377 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,379 software developers and data experts.

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 3402
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.__file__
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***@dalkescientific.com
Jul 18 '05 #4
Andrew Dalke <ad****@mindspring.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
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...
1
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...
3
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...
4
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,...
15
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...
2
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...
33
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...
11
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.