[melbourne-pug] python question

Andreux Fort andrew.fort at gmail.com
Tue Apr 3 04:25:37 CEST 2012


short answer: python is "pass by reference to value", meaning you should
follow Google's style guide and only import packages or modules, never
things inside modules

When you do "from x import y", and then somewhere else update "x.y" (via
import x), you're updating two different variable references.
 On Apr 3, 2012 12:05 PM, "Brian May" <brian at microcomaustralia.com.au>
wrote:

> Hello,
>
> The following code:
>
> === a ===
> import b
> import c
> b.init()
> print b.value
> c.action()
> === end ===
>
> === b.py ===
> value = None
> def init():
>    global value
>    value = "something"
> === end ===
>
> === c.py ===
> from b import value
> import b
>
> def action():
>    print value
>    print b.value
> === end ===
>
> generates the following output:
>
> === output ===
> something
> None
> something
> === end ===
>
> Question: Why is the second line None, and not something? Then, why is
> the third line something?
>
> This is kind of confusing me.
>
> I suspect it might be some sort of misunderstanding I have with
> Python. However I can't think of what that might be. I would have
> though "from b import value" and "import b" should give the same
> results.
>
> If I swap the order of "import c" and "b.init()" in a, then all the
> results are something.
>
> If I change the value in the line "value = None", then I get that
> value instead of None on the 2nd line - is like I have two copies of
> b.value.
>
> Thanks
> --
> Brian May <brian at microcomaustralia.com.au>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20120403/e591737d/attachment.html>


More information about the melbourne-pug mailing list