for convenience

dn PythonList at DancesWithMice.info
Mon Mar 21 18:04:53 EDT 2022


On 22/03/2022 10.17, Chris Angelico wrote:
> On Tue, 22 Mar 2022 at 08:13, 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?
>>
> 
> I don't understand the question. When you do that "for convenience"
> assignment, what you're doing is creating a local variable named
> "context" which refers to the same thing that bpy.context does (or did
> at the time of the assignment, but presumably you only do this when
> bpy.context won't get reassigned). It has no effect on any other name.
> There's no magic happening here - it's just assignment to the name
> context, like anything else.
> 
> What are you expecting to happen here?


It's the way Python works.

try:

context = bpy.context  # convenience
print( id(context), id(bpy.context) )

Remember that the 'relationship' between the two is established at
run-time and at the data/address 'level' - and not at compile-time. Thus
"context" points to a memory location, and does not 'stand for'
"bpy.context" anywhere other than in your mind.

(which is why we often need to use a copy() when we want 'separate data'
- see also the 'counters' Python uses to manage "garbage collection")

-- 
Regards,
=dn


More information about the Python-list mailing list