Confused about accessing segments in Python
Posted: Thu Oct 23, 2014 3:09 pm
Take the following session example:
1. Calling s.allseg() just returns s again. I think I understand why, but it is confusing for anyone trying to learn the API.
2. Why does list(s) return lists of different length for the first and second invocation?
I suspect that by calling s.allseg(), some internal flag of the Section is altered such that iterating over the Section will return the complete set of nodes expected from allseg() (http://www.neuron.yale.edu/phpbb/viewto ... f=2&t=2150), but obviously this results in some strange behavior if I don't immediately request all items in the iteration. A better approach might be for h.allseg() to return a dedicated iterator without changing the state of the Section.
Code: Select all
>>> s = h.Section()
>>> s.nseg
1
>>> s.allseg()
<nrn.Section object at 0x7f50c483fd50>
>>> list(s)
[<nrn.Segment object at 0x7f50bc8ab058>, <nrn.Segment object at 0x7f50bc8ab8c8>, <nrn.Segment object at 0x7f50bc8ab7d8>]
>>> list(s.allseg())
[<nrn.Segment object at 0x7f50bc8ab738>, <nrn.Segment object at 0x7f50bc8ab800>, <nrn.Segment object at 0x7f50bc8ab850>]
>>> list(s)
[<nrn.Segment object at 0x7f50bc8ab058>]
>>> list(s.allseg())
[<nrn.Segment object at 0x7f50bc8ab738>, <nrn.Segment object at 0x7f50bc8ab800>, <nrn.Segment object at 0x7f50bc8ab850>]
2. Why does list(s) return lists of different length for the first and second invocation?
I suspect that by calling s.allseg(), some internal flag of the Section is altered such that iterating over the Section will return the complete set of nodes expected from allseg() (http://www.neuron.yale.edu/phpbb/viewto ... f=2&t=2150), but obviously this results in some strange behavior if I don't immediately request all items in the iteration. A better approach might be for h.allseg() to return a dedicated iterator without changing the state of the Section.