I want to see all the variables

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Dec 30 21:47:31 EST 2006


On Sat, 30 Dec 2006 11:08:10 -0800, johnf wrote:

> Very detailed.  But I was attempting to debug some code which subclassed
> other code. I got a traceback that something like "no
> mySubClass.__source.query() did not exist".  The superclass had something
> like "myClass.__source.query(sql)" which worked
> but "mySubClass.__source.query(sql)" did not work.

That's right, exactly as expected.

As I said before, Python mangles the name of double-leading underscore
class attributes. So when the developer of myClass wrote 

myClass.__source.query(sql)

at compile time Python changes that to

myClass._myClass__source.query(sql)


When you write mySubClass.__source.query() it doesn't work because there
is no attribute "__source". You need to change that to "_myClass__source"
-- even when it appears in mySubClass.


> So I tried to debug
> using "dir(myClass.__source)" and got an error. And I also got error when
> I "dir(mySubClass.__source". So how could I have debugged the problem if
> dir() will not display information on the __source?  I hope that explains
> my issue.

This isn't actually a problem with dir -- although dir does sometimes hide
methods, this isn't one of those cases. This is a case of you not knowing
that Python mangles the name of __attributes. What you should have done is
call dir(myClass), in which case you almost certainly would have seen
_myClass__source listed.



-- 
Steven.




More information about the Python-list mailing list