Jump to content
iqrf

How to catch if the object is assigned None

Recommended Posts

Hi,
Let's take Demo08 as an example where a myPoint object is created in Delphi. I need to detect on the Delphi side if the assignment spam.myPoint = None is made in Python. Cyclic polling using PythonModule1.GetVar('myPoint') is inappropriate for me. I would need something like OnChange in TPythonDelphiVar. I've studied PythonEngine.pas and can't think of anything.
Thank you for your opinion.

Share this post


Link to post
function  TPyPoint.SetAttr(key : PAnsiChar; value : PPyObject) : Integer

is called whenever a property is set (e.g. spam.myPoint.x = 10).     So you can take an action when the object is modified.  

 

However you are not notified when  spam.myPoint is assigned a value.

 

To do what you would have to override the __setattr__ on the module, but this is too complicated.

Edited by pyscripter

Share this post


Link to post

Couldn't this be done at the Python level using a decorator above the module?

import types

class MyModule(types.ModuleType):
    def __setattr__(self, name, value):
        print(f"Setting attribute {name} to value {value}")
        super().__setattr__(name, value)

mymodule = MyModule('mymodule')
mymodule.my_attribute = 42
print(mymodule.my_attribute)

 

Share this post


Link to post

It's really not easy, so I'll accept the imperfection and try to solve it sometime in the future. Thanks

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×