Page 1 of 1

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
by watersun
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

Code: Select all

template_name = "templatename"                     -> string
I couldn't concatenate hocobjects with strings, so I wasn't able to do

Code: Select all

mycell = h.template_name
I tried many different ways to achieve the outcome. With exec, getattr or just using

Code: Select all

h("objref cell"); h("cell = new templatename")
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:

Code: Select all

mycell = getattr(h, template_name)(hoc_path) 
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 ~