You are not logged in.

#1 2013-04-01 21:32:37

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

[PEBKAC]Python3: manually declaring __name__

If I manually declare __name__ in a module then import it, module.__name__ is my declared name, but help() only works if called with the default name.  Just out of curiosity, I wonder what is the rationale behind this.

Edit: NVM. I overlooked the fact that I used fakemodule.__name__ (rather than newname.__name__ as I might have expected). So I guess __name__ has nothing to do with how the module or its contents are accessed.

fakemodule.py:

__name__ = 'newname'

python interpreter:

>>> import fakemodule
>>>print(fakemodule.__name__)
newname
>>> help(newname)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'newname' is not defined
>>> help(fakemodule)

help(fakemodule) output:

Help on module newname:

NAME
    newname

FILE
    /home/alphaniner/python3/fakemodule.py

Last edited by alphaniner (2013-04-02 13:25:25)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#2 2013-04-02 06:47:39

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [PEBKAC]Python3: manually declaring __name__

That's because help doesn't care about the __name__, it only cares about the object it takes as an argument, and in your code the object 'newname' does not exist. If you want to import fakemodule under a name newname, you can do it like this:

import fakemodule as newname

Last edited by kaszak696 (2013-04-02 06:48:02)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#3 2013-04-02 06:55:00

ismaelvc
Member
From: México, D.F.
Registered: 2012-02-26
Posts: 136

Re: [PEBKAC]Python3: manually declaring __name__

Indeed 'newname' is just a string assigned to the __name__ variable in your code, you can run some built-in functions like dir( ) to check the namespace, there is also globals( ) and locals( ). kaszak696 is right try passing 'newname' to help( ), notice the quotes that indicate it's a string:

help('newname')

as 'newname' is a string object you will get help for the string built-in type.

Last edited by ismaelvc (2013-04-02 07:07:52)


Laptop: LG LM70 Express                                                  Kernel: 3.14.2-1-ARCH   
CPU: Intel Pentium M processor @1.86GHz                  Hard Drive: 80G
Video: Mobility Radeon X600                                            X Driver: xf86-video-ati
Memory Size: 1.5G + zramswap: 384M + swap: 3G

Offline

#4 2013-04-02 13:24:51

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [PEBKAC]Python3: manually declaring __name__

ismaelvc wrote:
help('newname')

That didn't work in python 2 or 3:

>>> import fake
>>> help('newname')
no Python documentation found for 'newname'

However:

>>> help('fake.__name__')

Brought up a slightly 'customized' copy of help(str).  So I guess it's a matter of quoting the name of a string variable rather than its contents.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#5 2013-04-02 15:31:03

ismaelvc
Member
From: México, D.F.
Registered: 2012-02-26
Posts: 136

Re: [PEBKAC]Python3: manually declaring __name__

@alphaniner:

I'm sorry for not double checking, you are right, I forgot the usage of help() a bit:

help([object])
Invoke the built-in help system. (This function is intended for interactive use.)
If no argument is given, the interactive help system starts on the interpreter console. 
If the argument is a string, then the string is looked up as the name of a module,
function, class, method, keyword, or documentation topic, and a help page is 
printed on the console. If the argument is any other kind of object, a help page 
on the object is generated.

This function is added to the built-in namespace by the site module.

Note this is from python 3.3

As you can see strings are the exception, but you can pass any other object to the help function, like this:

help([1,2,3,4,5])
help({'uno': 'one', 'dos': 'two'})
help({'aa', 'bb', 'cc'})

lucky = 777
help(lucky)
>>> class test:
...     '''This is a test class.'''
...     def test_method():
...         '''This does nothing!'''
...         pass
...
>>> help(test)
Help on class test in module __main__:

class test(builtins.object)
 |  This is a test class.
 |
 |  Methods defined here:
 |
 |  test_method()
 |      This does nothing!
 |
 |  ------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)


>>> test_instance = test()
>>> help(test_instance)   # The same as above

As you can see even user defined types can be used with help(), as long as you properly document your code with docstrings ('''docstring use triple quotes''')!

alphaniner wrote:

So I guess it's a matter of quoting the name of a string variable rather than its contents.

Try this:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

I love this one:

In the face of ambiguity, refuse the temptation to guess.   wink

http://docs.python.org/3.3/library/

Last edited by ismaelvc (2013-04-02 16:51:08)


Laptop: LG LM70 Express                                                  Kernel: 3.14.2-1-ARCH   
CPU: Intel Pentium M processor @1.86GHz                  Hard Drive: 80G
Video: Mobility Radeon X600                                            X Driver: xf86-video-ati
Memory Size: 1.5G + zramswap: 384M + swap: 3G

Offline

#6 2013-04-02 16:42:11

ismaelvc
Member
From: México, D.F.
Registered: 2012-02-26
Posts: 136

Re: [PEBKAC]Python3: manually declaring __name__

You can also run help( ) interactively (with no arguments):

>>> help()

Welcome to Python 3.3!  This is the interactive help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.3/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help>

Last edited by ismaelvc (2013-04-02 16:57:14)


Laptop: LG LM70 Express                                                  Kernel: 3.14.2-1-ARCH   
CPU: Intel Pentium M processor @1.86GHz                  Hard Drive: 80G
Video: Mobility Radeon X600                                            X Driver: xf86-video-ati
Memory Size: 1.5G + zramswap: 384M + swap: 3G

Offline

Board footer

Powered by FluxBB