-1

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
GUIMish
  • 23
  • 6
  • 1
    Do you know how you would write that code in C++ without an assignment inside the `if` condition? – user2357112 Sep 20 '18 at 18:18
  • in c++ I wrote correctly, it adds gtk_paned to the variable, and if answers whether everything went well or not. – GUIMish Sep 20 '18 at 18:33

1 Answers1

1

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
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • It is possible to implement adding to a variable inside if? in python – GUIMish Sep 20 '18 at 18:23
  • No that was [explicitly disallowed](https://stackoverflow.com/questions/2603956/can-we-have-assignment-in-a-condition) by the language developer (Guido) in Python. In many C++ compilers, they will emit a warning if you do this as they think you may have done so accidentally – Cory Kramer Sep 20 '18 at 18:47