Odd behaviour on importing variable from module
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a module, oddmodule.py (below), that defines a variable, OddVariable, by assigning a value A to it. The file I execute, mainfile.py, imports and re-binds OddVariable to a value B. I have two test modules which import the OddVariable in two different ways, one via "import oddmodule" and another via "from oddmodule import OddVariable". The weird behaviour is that by importing using the former syntax, I can see OddVariable bound to B (as expected), but the latter syntax sees it bound A. Is this the intended behaviour? Am I misunderstanding what is going on here? Source code: <<>> print("Importing oddmodule") OddVariable = ["not", "initialized"] def OddFunction(): print("OddVariable from oddmodule.OddFunction:", OddVariable) <<>> import oddmodule import testmodule1 import testmodule2 print("Initializing OddVariable") oddmodule.OddVariable = ["some", "list"] print() testmodule2.DoTest() print() testmodule1.DoTest() <<>> from oddmodule import OddVariable, OddFunction def DoTest(): print("STARTING testmodule1.DoTest") print("OddVariable from testmodule1:", OddVariable) OddFunction() print("FINISHED testmodule1.DoTest") <<>> import oddmodule def DoTest(): print("STARTING testmodule2.DoTest") print("OddVariable from testmodule2:", oddmodule.OddVariable) oddmodule.OddFunction() print("FINISHED testmodule2.DoTest") OUTPUT: Importing oddmodule Initializing OddVariable STARTING testmodule2.DoTest OddVariable from testmodule2: ['some', 'list'] OddVariable from oddmodule.OddFunction: ['some', 'list'] FINISHED testmodule2.DoTest STARTING testmodule1.DoTest OddVariable from testmodule1: ['not', 'initialized'] !!! OLD VALUE !!! OddVariable from oddmodule.OddFunction: ['some', 'list'] FINISHED testmodule1.DoTest Many thanks, Roman -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour on importing variable from module
> the construct > > from oddmodule import OddVariable, OddFunction > > assigns the *values* of the given module names to new variables in the > importing module's namespace. that is, you're binding new names to the > values the variables happen to have when the from-import statement is > executed. Ah right, this does indeed explain what I'm seeing. For some reason I thought "from module import variable" magically makes "variable" an "alias", so to speak, for "module.var", which of course it does not. > Yes. Think of > > from module import var > > as a shortcut for > > import module > var = module.var > del module Good tip, I'll remember this. Thanks very much Fredrik, Peter! Roman -- http://mail.python.org/mailman/listinfo/python-list
Stuck connection in Python 3.0b2 http.server
Hi All I've encountered a weird issue when migrating a web server to Python 3 - the browser would wait forever without showing a page, displaying "Transferring data" in the status bar. I tracked it down to a reference cycle in my BaseHTTPRequestHandler descendant - one of the attributes stored a dict of methods. Removing the cycle made the problem go away. In Python 2.5.2 the code works fine either way. Here's a minimal example which runs in both 2.5 and 3.0 - to see stuck connections run as-is in 3.0 and navigate to http://localhost:8123; to fix this comment out "self.dummy = self" (alternatively reset self.dummy = None at the end of the __init__ method). Am I doing it wrong, or is this a bug? try: import http.server httpmodule = http.server except: import BaseHTTPServer httpmodule = BaseHTTPServer class BreakageRequest(httpmodule.BaseHTTPRequestHandler): def __init__(self, request, client_address, server): self.dummy = self # COMMENT THIS OUT to see the connection become unstuck httpmodule.BaseHTTPRequestHandler.__init__(self, request, client_address, server) def do_GET(self): self.send_response(200) self.send_header("Content-Type", "application/xml") self.end_headers() self.wfile.write("".encode('utf-8')) srv = httpmodule.HTTPServer(('', 8123), BreakageRequest) srv.serve_forever() -- http://mail.python.org/mailman/listinfo/python-list
Re: How to emit Cyrillic and Chinese via unicode from console mode?
On Sep 14, 2:03 am, "Siegfried Heintze" <[EMAIL PROTECTED]> wrote: > Can someone point me to an example of a little program that emits non-ascii > Unicode characters (Russian or Chinese perhaps)? The following doesn't quite work, but I'll post it anyway since it actually ends up printing the characters. Perhaps someone can point out what causes the exception at the end? The important thing is to set the console codepage to 65001, which is UTF-8. This lets you output utf8-encoded text and see the Unicode chars displayed. import sys import encodings.utf_8 import win32console sys.stdout = encodings.utf_8.StreamWriter(sys.stdout) win32console.SetConsoleCP(65001) win32console.SetConsoleOutputCP(65001) s = "English: ok\n" s += u'Russian: \u0420\u0443\u0441\u0441\u043a\u0438\u0439\n' s += u'Greek: \u03bc\u03b5\u03b3\u03b1\u03bb\u03cd \u03c4\u03b5\u03c1\u03b7\n' print s If redirected to file, all is well, this prints everything properly in UTF-8. If ran on the console, this also prints everything correctly, but then throws a mysterious exception: English: ok Russian: Русский Greek: μεγαλύτερη Traceback (most recent call last): File "I:\Temp\utf8console.py", line 18, in print s File "C:\Progs\Python25\lib\codecs.py", line 304, in write self.stream.write(data) IOError: [Errno 0] Error Any ideas? Roman P.S. This really ought to Just Work in this day and age, and do so without all those 65001/utf8 incantations. Pity that it doesn't. Sigh. -- http://mail.python.org/mailman/listinfo/python-list
Re: recursion gotcha?
On Sep 14, 9:01 am, cnb <[EMAIL PROTECTED]> wrote: > def suma(xs, acc=0): > if len(xs) == 0: > acc > else: > suma(xs[1:], acc+xs[0]) > > it returns none. Yep, that's because there is no "return" statement anywhere. Python doesn't return expressions "by default", like functional languages do, so where you say "suma(xs[1:], acc+xs[0])" this just calls itself and returns nothing. Try this: def suma(xs, acc=0): if len(xs) == 0: return acc else: return suma(xs[1:], acc+xs[0]) print suma([1, 2, 3, 4, 5]) (prints 15) Roman -- http://mail.python.org/mailman/listinfo/python-list
Re: How to emit Cyrillic and Chinese via unicode from console mode?
On Sep 14, 11:51 am, Gertjan Klein <[EMAIL PROTECTED]> wrote: > Interesting. On my system (Windows XP) the console codepage does not > change, and hence the characters don't print properly (I get some of the > CP437 line drawing characters, for example). I have never been able to > convince windows to assume/support UTF-8 encoding in the console, > programatically or otherwise. :( I found that a useful test is to create a directory whose name contains chars from various languages, then run "cmd" and see if "dir" displays them correctly. This worked fine for me in WinXP, even though my system locale is Russian. Would be interesting to know if you can get the console to display international chars this way. -- http://mail.python.org/mailman/listinfo/python-list
Add vs in-place add of str to list
Hi I'm trying to understand why it is that I can do >>> a = [] >>> a += 'stuff' >>> a ['s', 't', 'u', 'f', 'f'] but not >>> a = [] >>> a = a + 'stuff' Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "str") to list Can someone explain the logic? Why is the in-place add not a type error but the usual add is? (This applies to both Python 2.6rc1 and 3.0b2) Thanks Roman -- http://mail.python.org/mailman/listinfo/python-list
Re: Add vs in-place add of str to list
On Oct 2, 8:11 am, Erik Max Francis <[EMAIL PROTECTED]> wrote: > It's because the `+=` operator is doing the equivalent of calling the > `extend` method, which treats its argument as a generic sequence, and > doesn't enforce type. I see. Do you know whether this is seen as a problem with the language design? If so, this could be fixed by changing list.__add__ to call .extend on the left argument if it's a list... Or, by changing __iadd__ to throw a TypeError, whichever way around is seen as more correct/desirable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Add vs in-place add of str to list
On Oct 2, 3:50 pm, Mel <[EMAIL PROTECTED]> wrote: > rs387 wrote: > > I see. Do you know whether this is seen as a problem with the language > > design? > > No. OK, I get it now. I was assuming that the "+" could be implemented in terms of "+=" as follows: def add(x,y): temp = list(x) temp += y return temp But it turns out that this is not what "+" means at all. A bit of a gotcha... Roman -- http://mail.python.org/mailman/listinfo/python-list