__bases__ misleading error message
Ian Kelly
ian.g.kelly at gmail.com
Sat Jan 24 16:45:32 EST 2015
On Sat, Jan 24, 2015 at 2:14 PM, Mario Figueiredo <marfig at gmail.com> wrote:
> In article <54c39366$0$13006$c3e8da3$5496439d at news.astraweb.com>,
> steve+comp.lang.python at pearwood.info says...
>> > AttributeError: 'Sub' instance has no attribute '__bases__',
>> > AttributeError: 'foo' object has no attribute '__bases__'
>>
>> The first would be nice. The second is impossible: objects may have no name,
>> one name, or many names, and they do not know what names they are bound to.
>> So the Sub instance bound to the name 'foo' doesn't know that its name
>> is 'foo', so it cannot display it in the error message.
>
> Thanks for the information! :)
>
> But that begs the OT question:
No, it doesnt. http://en.wikipedia.org/wiki/Begging_the_question
> How does Python maps names to memory
> addresses in the interpreter?
Global variables are looked up in the current stack frame's globals dict.
>>> a = 1
>>> b = 2
>>> globals()['a']
1
>>> globals()['b']
2
Local variables of functions could be handled the same way, but for
efficiency the compiler instead maps the names to indices of a local
variable array associated with the stack frame. Either way, at the C
level the value stored in the dict or array is a pointer to the memory
location of the object.
> "__main__"
> from module import a_name
> y = a_name + 1
>
> How does python interpreter know how to map 'name' to the correct memory
> location, if this __main__ code is only ran after 'module' code?
I'm not sure I'm understanding what you're asking, but the import
statement imports the module, looks up "a_name" in that module's
globals dict, and binds the same object to a_name in the current
module's globals dict.
More information about the Python-list
mailing list