On Mon, Sep 25, 2017 at 10:46 AM, Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> Chris Angelico <ros...@gmail.com> writes:
>>On Mon, Sep 25, 2017 at 10:03 AM, Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>>>What's the difference between »builtins« and »_sitebuiltins«?
>>>|>>> type.__module__
>>>|'builtins'
>>>|
>>>|>>> help.__module__
>>>|'_sitebuiltins'
>>def sethelper():
>>    builtins.help = _sitebuiltins._Helper()
>
>   Thank you!
>
>   I still have a related question.
>
>   When I say,
>
> from math import floor
> help()
>
>   , Python will try to understand »help« and search for it in
>   the current scope. It will find a »help« there, as if after
>
> from builtins import *
>
>   . Has it somewhere retained the information that »help« came
>   from »builtins«, so that one can write a function (or some
>   similar means) as follows:
>
> wherefrom( 'help' )
>
>   will return
>
> builtins
>
>   or
>
> wherefrom( 'floor' )
>
>   will return
>
> math
>
>   ?

Yes and no. You can't find out where you actually got something,
because that 'something' is the same thing whereever you got it; but
you CAN find the "canonical source" of something. Examples:

>>> math.__name__
'math'
>>> import os
>>> os.__name__
'os'

So far, so good.

>>> import curses.ascii
>>> curses.ascii.__name__
'curses.ascii'

Works for package modules too. Great!

>>> import os.path
>>> os.path.__name__
'posixpath'

Ah. Because 'os.path' actually comes from somewhere else. And it's the
same module as if I said:

>>> import posixpath
>>> posixpath.__name__
'posixpath'
>>> os.path is posixpath
True

The same applies to functions.

>>> from math import floor
>>> floor.__module__
'math'

And classes.

>>> from enum import Enum
>>> Enum.__module__
'enum'

But if anything got imported from somewhere else, you get the
_original_ source, not the one you got it from:

>>> from ssl import namedtuple
>>> namedtuple.__module__
'collections'

Is that good enough for what you need? It's not "where did this come
from", but it's "where does this live".

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to