for convenience

Cameron Simpson cs at cskk.id.au
Mon Mar 21 18:02:49 EDT 2022


On 21Mar2022 22:12, Paul St George <email at paulstgeorge.com> wrote:
>When I am writing code, I often do things like this:
>
>context = bpy.context  # convenience
>
>then whenever I need bpy.context, I only need to write context
>
>
>Here’s my question:
>
>When I forget to use the convenient shorter form
>
>why is bpy.context not interpreted as bpy.bpy.context?

Because it still has its original meaning. You haven't changed the 
meaning of the word "context" in any position, you have simply made a 
module level name "context" referring to the same object to which 
"bpy.context" refers.

So your module global namespace contains, initially, "bpy". Then you 
assign:

    context = bpy.context

and now your module global namespace contains "bpy" and "context". But 
"bpy.context" is still what it was, because "bpy" is an object and you 
have done nothing to its ".context" attribute.

Consider this code:

    class O:
        pass

    o = O()
    o.x = 3

    x = o.x

    print(x)
    print(o.x)

I expect to see "3" twice. What do you expect?

"bpy" is no different to "o" - it's just a name.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list