for convenience
Paul St George
email at paulstgeorge.com
Tue Mar 22 16:39:14 EDT 2022
On 21/03/2022 18.02, Cameron Simpson wrote:
> 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.
Thanks Cameron,
What did I expect? Well, I expected to see "3” twice, but I did not understand why. Now I do!
—
Thanks,
Paul
More information about the Python-list
mailing list