[Tutor] Questions regarding class documentation ...
hcohen2
hcohen2 at comcast.net
Mon Jan 26 13:36:36 EST 2004
Sorry not sure what your is to address you by. However, your
explanation does take care of why the straight import <file name> did
not work. I used the from <file name> import <class name> and it did
work. Nonetheless, having seen so much discussion on why one should not
use from ... import ... I thought it odd this seemed necessary.
I am, however, still bothered by my seeing an instance type()ed as a
class and not an instance.
Thanks,
Herschel
orbitz at ezabel.com wrote:
>When you import a module, you tell python that you will be using these symbols. On the 'import' statement, the symbols in your module are not loaded into the global namespace. This is good, since there is always the change of two modules having the same class/function names in their namespace and you don't want them to conflict. so when you do import sampledict, it loads the module but all of the object it defines are located in the sampledict namespace. If you were sure you wanted defaultdict to be imported into the global namespace you could do:
>
>from sampledict import defaultdict.
>
>now your print defaultdict will work.
>
>I hope this helps.
>
>
>On Mon, 26 Jan 2004 09:33:57 -0500
>hcohen2 <hcohen2 at comcast.net> wrote:
>
>
>
>>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.
>>
>>
>>_______________________________________________
>>Tutor maillist - Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
More information about the Tutor
mailing list