Python: double underscore and base container iter
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 09:26, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 12
Platform: Mac OSX ;
Language(s) : PYTHON ;---------
I'm learning how to iterate through a container to get its IDs and Values.Question: Why do I not see the double underscore used in the example code, if the example code is supposed to be an example of how to use the BaseContainer.__iter__() function?
The SDK lists this member of the BaseContainer class as a way to iterate through the container:
BaseContainer.\__iter\_\_
()[](file:///Users/thuesen/Desktop/Python/modules/c4d/BaseContainer/index.html#BaseContainer.__iter__)The BaseContainer is iteratable so just use it like in the following example:
def iter_container(bc) : for index, value in bc: print "Index: %i, Value: %s", index, str(value)
Btw, the print line doesn't work. It seems to work if it's changed to:
print "Index: %i, Value: %s" % (index, str(value))
Thanks for any help,
Arik -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 12:17, xxxxxxxx wrote:
Hi ernia,
the for loop implicit calls __iter__ / iter(..), that's why its not in the example code.
So __iter__ means the class supports the iteration interface.Btw, the print line doesn't work. It seems to work if it's changed to:
print "Index: %i, Value: %s" % (index, str(value))
Correct, the braces were missing. Thx for this report.
Cheers, Sebastian
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 12:33, xxxxxxxx wrote:
Thanks for responding, Sebastian.
I don't believe I have enough of an understanding to follow your response.The fact that the letters "iter" are in the function name iter_container() is coincidence, right(?), because the the function name could be named anything and still work the same.
Are you saying that, somehow, when the BaseContainer class sees a "for loop" it knows exactly what you want it to do? If so, how does that work?
noob, noob
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 12:55, xxxxxxxx wrote:
The fact that the letters "iter" are in the function name iter_container() is coincidence, right(?), because the the function name could be named anything and still work the same.
Correct!
Are you saying that, somehow, when the BaseContainer class sees a "for loop" it knows exactly what you want it to do? If so, how does that work?
The foor loop just helps you browse through the BaseContainer, so you get the index and the corresponding value.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 13:06, xxxxxxxx wrote:
I can see that the for loop iterates through the IDs and values of the (bc) base container. I'm with you there.
But what I don't understand is what is BaseContainer.__iter__() and how does it work? What is the significance of the double underscores?
I'm having trouble understanding why I can't call BaseContainer.__iter__(bc) or bc.__iter__(), something like that, in a for loop and get the IDs and Values in that container. I think you're saying that the double underscores mean something is implied and not to be used as shown?
thanks.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 14:47, xxxxxxxx wrote:
In Python some names start and end with two underscores so they have a special meaning. If such function belongs to a class it means the type supports a special interface.
For instance: __iter__ (iterator), __len__ ( returns the length ), __repr__ (serialize an object)
Originally posted by xxxxxxxx
I'm having trouble understanding why I can't call BaseContainer.__iter__(bc) or bc.__iter__(), something like that, in a for loop and get the IDs and Values in that container.
That's wrong, you can call them, but the for loop simply calls it internally so its simply shorter. But I guess __iter__ is not exactly what you actually want to use. To set and get values at a specific index you can use:
bc[index] = value
this is the interface for __setitem__
or:
print bc[index]
this is the interface for __getitem__. All these methods are typelesss but you can also use the C++ way in Python like:
bc.GetString(index) Cheers, Sebastian
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 07/10/2010 at 17:14, xxxxxxxx wrote:
Thanks for taking the time, Sebastian.
Beginning to get the picture now.The "interface" is coded into the class, it seems. Looks like you've just made things easier for the user by soft coding some special methods, which use the double underscores, into the class itself, and when the user wants to use these special methods then he has to use the special "interface" that's specific for that method.
Appreciate your patience.
Arik