[Tutor] Questions regarding class documentation ...
hcohen2
hcohen2 at comcast.net
Mon Jan 26 09:33:57 EST 2004
Because the Core Python Programming book was so out of date regarding
class and objects discussion I switched over to reading the Alan Gauld
tutorial on the topic; afterward I directed my attention to the
documentation on the python.org site.
[ Here are some facts to keep in mind as you read the discussion below:
documentation was for version 2.2.3 and I am using 2.2.2. I have so far
reproduced the results exactly when I ran it interactively. I am trying
to prepare myself to create applications independently, hence, I try to
go beyond the text samples and use more general approaches.]
Rather than running the code, in section: Subclassing built-in types,
interactively I pasted the class definition in a file. Moreover, to
keep it general I gave the file a name different from the class defined
in the file, since I would expect I would have many unrelated class
definitions within a single file that I would load to run a custom
application.
So within the python interpreter I imported the file containing the
defaultdict class:
>>>import sampledict # name sampledict.py
however, when I attempted to run the sample code in the documentation:
>>> print defaultdict
...
NameError: name 'defaultdict' is not defined
Running dir() shows, indeed, 'sampledict' is listed but not
'defaultdict, despite the importing of the file that contains the
latter's definition.
Perhaps what I did next was due to my having a tenuous grasp on this
topic I remembered the problems with unbound method discussion. Hence,
I tried to created an instance, but I got the same error. At that point
I became suspicious of the success the importing command. Though I know
it is bad style and a command to be avoided I gave the command:
>>> from sampledict import defaultdict
>>> print defaultdict
<class 'sampledict.defaultdict'>
>>> dictIns = defaultdict()
>>> print dictIns
{}
>>> type(dictIns)
<class 'sampledict.defaultdict'>
Now the print dictIns 'works', but I have a number of questions!
Again the interactive reproduction of the code works exactly as
described (as far as I have gone). While I can reproduce the results,
why doesn't the simple import command work for the file? What happened
to seeing an instance rather than the type now being class for dictIns?
Could someone enlighten me regarding these questions without citing
further documentation that might just add to my confusion as to what may
be happening. When I created instances previously that were identified
as instances not as a class. [Regarding the last statement: on this
machine with this version of Python.]
It is very difficult to gain a firm grasp of this topic when the results
are so haphazard and not internally consistent.
I would like to thank anyone that can clarify why I have run into these
difficulties.
More information about the Tutor
mailing list