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

isinstance() bug

hello,

please consider the following situation:

under the current directory there's a subdirectory 'package' with two
files: __init__.py and module.py

./package:
__init__.py
module.py

module.py contains:

class A(object):
pass

aModule = A()
now, let's do:

current directory:
import package.module
type(package.module.aModule) <class 'package.module.A'> isinstance(package.module.aModule, package.module.A) # so far good 1 a = package.module.A()
isinstance(a, package.module.A) # so far good
import sys
sys.path.append('package')
import module
a = module.A()
isinstance(a, package.module.A) # will return 0 !!! 0 isinstance(package.module.aModule, module.A) # will return 0 !!! 0


how is it possible that it IS important how you imported a class
definition for isinstance() to work? it's insane!

--
fuf (fu*@mageo.cz)

Jul 18 '05 #1
6 2310

"Michal Vitecek" <fu*@mageo.cz> wrote in message
news:ma**************************************@pyth on.org...
hello,

please consider the following situation:

under the current directory there's a subdirectory 'package' with two
files: __init__.py and module.py

./package:
__init__.py
module.py

module.py contains:

class A(object):
pass

aModule = A()
now, let's do:

current directory:
>>> import package.module
>>> type(package.module.aModule) <class 'package.module.A'> >>> isinstance(package.module.aModule, package.module.A) # so far good 1 >>> a = package.module.A()
>>> isinstance(a, package.module.A) # so far good
>>> import sys
>>> sys.path.append('package')
>>> import module
>>> a = module.A()
>>> isinstance(a, package.module.A) # will return 0 !!! 0 >>> isinstance(package.module.aModule, module.A) # will return 0 !!! 0 >>>


how is it possible that it IS important how you imported a class
definition for isinstance() to work? it's insane!

--
fuf (fu*@mageo.cz)


its got to do with how python imports modules.
both imports are treated as seperate modules because the relative paths are
diferent.
python is a dynamic language it figures out where modules are located at run
time, it would be expensive to check all paths to see if they ended up at
the same file.
Jul 18 '05 #2

Sidharth> python is a dynamic language it figures out where modules are
Sidharth> located at run time, it would be expensive to check all paths
Sidharth> to see if they ended up at the same file.

Maybe not:

Help on function abspath in module posixpath:

abspath(path)
Return an absolute path.

Considering all the other stuff import has to do, this seems like only a
small extra bit of work.

Skip

Jul 18 '05 #3
i dont know unix.
how does abs path work with sym-links.

Jul 18 '05 #4
Stupid post must be ignored at all costs

"Sidharth Kuruvila" <si*******@hotmail.com> wrote in message
news:bv************@ID-223265.news.uni-berlin.de...
i dont know unix.
how does abs path work with sym-links.

Jul 18 '05 #5

Sidharth> i dont know unix.
Sidharth> how does abs path work with sym-links.

Not well. Seems that os.path.realpath does the right thing though:

% ls -l /etc/rc2.d
lrwxrwxrwx 1 root root 10 May 21 2002 /etc/rc2.d -> rc.d/rc2.d/
% python
Python 2.2.3 (#1, Jun 21 2003, 08:08:22)
[GCC 3.0.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
os.path.abspath("/etc/rc2.d") '/etc/rc2.d' os.path.realpath("/etc/rc2.d") '/etc/rc.d/rc2.d'

As someone else pointed out, the issue of multiple hard links to the same
file is a bit tougher nut to crack.

% cd ~/tmp
% cat > a.py
class A: pass
% ln a.py b.py
% python
Python 2.2.3 (#1, Jun 21 2003, 08:08:22)
[GCC 3.0.1] on linux2
Type "help", "copyright", "credits" or "license" for more information. import a
import b
a.A == b.A 0

You need to compare inode numbers to see if you have the same or different
files.
import stat
os.stat(a.__file__)[stat.ST_INO] 37041L os.stat(b.__file__)[stat.ST_INO]

37041L

Skip

Jul 18 '05 #6
On Wed, Jan 28, 2004 at 10:37:39PM +0530, Sidharth Kuruvila wrote:
i dont know unix.
how does abs path work with sym-links.

os.abspath does not check whether any components are symlinks. In the
presence of symlinks,
/a/../b
and
/b
can refer to separate files if /a is a symlink to a directory. As far as
I know, posixpath.abspath normalizes the first into the second anyway.

Similarly, if you have a symlink
/a -> c
then posixpath.abspath("a") doesn't return /c, it still returns /a

You can use os.readlink() on posix systems to get the thing a symlink
points to:
os.readlink("/proc/self") '14717' os.readlink("/")

OSError: [Errno 22] Invalid argument: '/'

Jeff

Jul 18 '05 #7

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

Similar topics

5
by: Jordan Rastrick | last post by:
Hi everyone, Just a little issue that I've come across in Python where I'd be interested to read the thoughts and opinions of the great thinkers and programmers who frequent this newsgroup. ...
12
by: codefire | last post by:
Hi, I'm using the isinstance built-in function. I've found the docs for it, but there are no docs on the supported types. For example isinstance(a, int) works fine but isinstance(s, string)...
7
by: Tlis | last post by:
I am using a software system with an embedded Python interpreter (version 2.3) for scripting. The KcsPoint2D.py module contains a Point2D class with the following method: def...
5
by: Boris Borcic | last post by:
Given the ABC innovation, maybe an infix syntax for isinstance() would be good. Possibilities : - stealing "is" away from object identity. As a motivation, true use cases for testing object...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
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...

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.