run code "at import time" for example, when a class gets added to the path

20 vues (au cours des 30 derniers jours)
Adam
Adam le 18 Nov 2025 à 9:26
Commenté : Rik il y a environ 20 heures
In python, I find it a useful feature to run some code when a module gets imported. For example, to register the import function of a class in a list of importers:
class Demo
# some stuff
def import(data : dict)-> Demo
# blabla
register_importer(Demo,import)
How should I do this in Matlab? As far as I know, there is no mechanism to run some code 'at import time' i.e. when a class gets added to the path.
Is there maybe such a mechanism in a namespace? For example: assume I have the following namespace:
+my_namespace
Demo.m
import.m
init.m
now when I add this namespace to my path, it will automatically run the init.m script to register importer.
  1 commentaire
Steven Lord
Steven Lord il y a environ 10 heures
Can you say a little more about what you would want to do with this init.m script on importing if this functionality existed? It's possible that there is an alternate way to achieve your goal.
Also, would you want this initialization code to be run whenever the folder containing the namespace folder was added to the path, whenever you actually called import to add the namespace to the import list, whenever you cd to the namespace folder's parent folder to make it accessible, or all of the above?

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 19 Nov 2025 à 9:56
My personal solution is to use something like GetWritableFolder. I then load the defaults from a file in that folder. If the file/folder doesn't exist, the function didn't run before, or the output didn't persist.
It isn't quite the same as running code on import, but for my purposes import and first run are generally equivalent.
Note: to solve performance issues I generally store a success flag in a persistent variable. That doesn't persist between sessions, but it helps with repeated calls.
  2 commentaires
Adam
Adam il y a environ 12 heures
Modifié(e) : Adam il y a environ 12 heures
So you would do it with a persistent variable in the constructor basically:
classdef Demo
methods
function self = Demo()
persistent is_registered
if isempty(is_registered)
register_importer(blabla)
is_registered = true
end
end
end
end
Too bad that this pollutes the actual code of the class with things that are not really related to its definition. Also, I guess a subclass can forget to call its superclass constructor and then it would break.
Rik
Rik il y a environ 8 heures
Essentially, yes.
I don't know if I completely agree with you on the second part. If some code needs to run on install/import, doesn't it make sense to have that code in your class defenition? It is arguably a component of your class (since it is apparently required to have run before you use it).
Also, is the risk you identify reasonable? Since the code only has to run once? That would mean that a subclass should forget to call the superclass constructor and the superclass should not have been called at all, ever. I don't mean to ask this as a rhetorical question. It might very well be a real risk in your situation.
This would also all depend on the answers you will give to Steven.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by