Why does this Python Hoc Cell Instantiation from Hoc with getattr() work after adding (morpho_path) behind.
Posted: Sun Jun 20, 2021 12:00 pm
Hello all,
I was trying to use a dynamically read in template_name with a spellcheck for "begintemplate" in the corresponding hoc morphology file to create python cell variables.
While this name extraction works and I retrieve the correct template_name, I tried to create a python cell with it, which was more difficult than expected.
So I have
I couldn't concatenate hocobjects with strings, so I wasn't able to do
I tried many different ways to achieve the outcome. With exec, getattr or just using where, with the latter one, I wasn't able to create a python variable with it instead of just having a new created cell in the neuron scope.
Well, gladly I found a solution, but I don't know why it works. The final one which was working was:
where hoc_path is the path to the hoc file.
Now I would really want to know, and therefore I ask here, if you know why it works with the path in the second braces behind the getattr(). Why does just giving the hoc path to the h.template_name method instantiate the cell, and why is it not doing it without the hoc_path? I know that it is basically the same like calling h.templatename(hoc_path), but why does it need that?
Best ~
I was trying to use a dynamically read in template_name with a spellcheck for "begintemplate" in the corresponding hoc morphology file to create python cell variables.
While this name extraction works and I retrieve the correct template_name, I tried to create a python cell with it, which was more difficult than expected.
So I have
Code: Select all
template_name = "templatename" -> string
Code: Select all
mycell = h.template_name
Code: Select all
h("objref cell"); h("cell = new templatename")
Well, gladly I found a solution, but I don't know why it works. The final one which was working was:
Code: Select all
mycell = getattr(h, template_name)(hoc_path)
Now I would really want to know, and therefore I ask here, if you know why it works with the path in the second braces behind the getattr(). Why does just giving the hoc path to the h.template_name method instantiate the cell, and why is it not doing it without the hoc_path? I know that it is basically the same like calling h.templatename(hoc_path), but why does it need that?
Best ~