I have a problem. I working in c++ and i dont know what i do wrong in python.
I have a c++ code:
if ((main_paned = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL)))){
std::cout << "created" << std::endl;
}
and i created this code in python:
if ((main_paned = gtk.HPaned())):
print "created";
but return: SyntaxError: invalid syntax.
What i do wrong? And how to create add to variable in if
?
The C++ version is using a side-effect to do both an assignment and a comparision at the same time. This works because the assignment operator happens to also return the value that it assigned, which is what the if
is using for the truthiness comparison. To break it into two steps, it would be like doing:
main_paned = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL));
if (main_paned)
{
// rest of code
}
Python does not allow this behavior so you'd have to do a similar thing
main_paned = gtk.HPaned()
if main_paned:
# code
or just
if gtk.HPaned():
That is of course assuming that your C++ code is correct, and you didn't instead intend to perform a logical comparison (==
) instead of an assignment (=
)
if (main_paned == GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL)))
{
// rest of code
}