Spaß mit import from in Python

  • warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_poll_menu_access' not found or invalid function name in /www/htdocs/w0062791/arnebab/includes/menu.inc on line 455.
  • warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_poll_menu_access' not found or invalid function name in /www/htdocs/w0062791/arnebab/includes/menu.inc on line 455.

→ Antwort auf Is it in this case : http://identi.ca/url/75523035 (see : [01:16]1 — Julien-Claude Fagot, die eine Antwort war auf One more reason why you should not use “from bla import foo”: print __import__(obs.__class__.__module__).__file__ — ArneBab

Datei: bla.py

def foo():
    print "bla"

Interaktiver Test:

\>>> import bla
\>>> bla.foo()
bla
\>>> def fu():
...   print "fu"
...
\>>> fu()
fu
\>>> from bla import foo
\>>> foo()
bla
\>>> bla.foo = fu
\>>> bla.foo()
fu
\>>> foo()
bla

Profifrage: Was passiert, wenn du from bla import foo nach bla.foo = fu ausführst?

Antwort:

\>>> import bla
\>>> bla.foo()
bla
\>>> def fu():
...   print "fu"
...
\>>> fu()
fu
\>>> bla.foo = fu
\>>> bla.foo()
fu
\>>> from bla import foo
\>>> foo()
fu

Happy hacking!


  1. dsop: if you use 'bla.foo', then yes, you can assign to bla.foo and you'll see the change. If you do 'from bla import foo', then your locally imported 'foo' will not 'see' changes to bla.foo. 

Use Node:

⚙ Babcom is trying to load the comments ⚙

This textbox will disappear when the comments have been loaded.

If the box below shows an error-page, you need to install Freenet with the Sone-Plugin or set the node-path to your freenet node and click the Reload Comments button (or return).

If you see something like Invalid key: java.net.MalformedURLException: There is no @ in that URI! (Sone/search.html), you need to setup Sone and the Web of Trust

If you had Javascript enabled, you would see comments for this page instead of the Sone page of the sites author.

Note: To make a comment which isn’t a reply visible to others here, include a link to this site somewhere in the text of your comment. It will then show up here. To ensure that I get notified of your comment, also include my Sone-ID.

Link to this site and my Sone ID: sone://6~ZDYdvAgMoUfG6M5Kwi7SQqyS-gTcyFeaNN1Pf3FvY

This spam-resistant comment-field is made with babcom.

Inhalt abgleichen
Willkommen im Weltenwald!
((λ()'Dr.ArneBab))



sn.1w6.org news

Draketo neu: Beiträge