2011-12-14 11 views
6

PythonからC++に渡されるリストの長さを計算する方法はありますか?boost :: python :: list length

class Awesome{ 
    public: 
    void awesomeMethod(const boost::python::list& list_of_something){ 
     list_of_something.length() // suprisingly there's no such method 
    } 
}; 

答えて

16

Like Python、あなたは長さを取得するためにfree function len()を使用する必要があります方法私はこのような 何かをしないしたいが、リストクラスはlength(または同様のもの)を欠いています。それはないlengthlenと、呼ばれています

boost::python::len(list_of_something) 
2

試してみてください、そして、それは方法が、自立機能はありません(Pythonはlengthメソッドを使用しませんが、長さプロトコルとlen()機能)。

return boost::python::len(list_of_something);