[Tutor] Questions regarding class documentation ...
Lloyd Kvam
pythontutor at venix.com
Mon Jan 26 16:41:51 EST 2004
Sorry about this post. I misread what you had done. For now old-style classes
and new style classes are both available in Python, but give slightly
different type information. Below class C is an old-style class and
D is a new-style class. I hope this makes it clear.
>>> class C:
... pass
...
>>> class D(object):
... pass
...
>>> c = C()
>>> d = D()
>>> type(c)
<type 'instance'>
>>> type(d)
<class '__main__.D'>
Lloyd Kvam wrote:
> Isn't defaultdict a class?
>
> Can you enter(?):
> dd = defaultdict()
>
> I expect type(dd) would then be an instance.
>
> Also try doing:
> type(C)
> and
> type(c)
>
> hcohen2 wrote:
>
>> Lloyd,
>>
>> Works fine as you described, thanks! Have any idea why I am not
>> seeing seeing the instance typed properly?
>>
>> >>> import sampledict
>> >>> dictIns = sampledict.defaultdict()
>> >>> type(dictIns)
>> <class 'sampledict.defaultdict'>
>>
>> What is the difference here, other than the simplicity of the class
>> definition?
>>
>> >>> class C:
>> ... print 'This is class C'
>> ...
>> This is class C
>> >>> c = C()
>> >>> type(c)
>> <type 'instance'>
>>
>> Is this explained by metaclass programming: "... Any class whose
>> instances are themselves classes, is a metaclass. When we talk about
>> an instance that's not a class, the instance's metaclass is the class
>> of its class ..."
>>
>> Not crystal clear to me yet, but I am beginning to think that many of
>> the other times I have seen instances they were really derived from
>> the old built-in types. That is, the instance is itself a simple
>> number (could be complex) or string type object.
>>
>> Not sure I am really happy with this, but this may be route to my
>> understanding this seeming discrepancy.
>>
>> Thanks,
>> Herschel
>>
>>
>> Lloyd Kvam wrote:
>>
>>> After importing a module, the "stuff" the module provides is referenced
>>> from within the module's namespace.
>>>
>>> What you want is:
>>>
>>> import sampledict
>>>
>>> print sampledict.defaultdict
>>>
>>>
>>> In general each python source file represents a separate namespace.
>>>
>>> hcohen2 wrote:
>>>
>>>> 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
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Tutor maillist - Tutor at python.org
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>
>>
>>
>>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice: 603-653-8139
fax: 801-459-9582
More information about the Tutor
mailing list