MFE: To do or not to do?

I wouldn't use the word "flexible".

I would. But that's allowed.

BTW the Java/C++ war died some time ago.

Programmers can be very clannish at times.

Who cares, really? :D
 
I knew Java first. Then C++. Then Python. But I feel after programming in Python for 2 years that it's a great first language to learn and prefer to have learned this first than going to the JAVA route.

I agree. First learn to think like a programmer, then learn the intricacies of memory management etc.
 
I knew Java first. Then C++. Then Python. But I feel after programming in Python for 2 years that it's a great first language to learn and prefer to have learned this first than going to the JAVA route.
so please help me with this:
I need the equivalent structure in python of these C structures:

do{
// my code
}while(exp?);

I want to translate some C code to python, also I need the equivalent python code for this

switch(?){
case i: //code ; break;
....
}

try not to google, no satisfying equivalent code found so far
thanks in advance
xsn
 
so please help me with this:
I need the equivalent structure in python of these C structures:

do{
// my code
}while(exp?);

I want to translate some C code to python, also I need the equivalent python code for this

switch(?){
case i: //code ; break;
....
}

try not to google, no satisfying equivalent code found so far
thanks in advance
xsn

those constructs don't really exist in python but, as usual, they are all syntactic sugar for something else.
  • do ... while can be implemented either with a loop or a tail recursive function
  • the switch is implemented using a dictionary and function calls or multiple if...elif...else.
By the way, translating literally from one language to another is a very bad idea.
 
those constructs don't really exist in python but, as usual, they are all syntactic sugar for something else.
  • do ... while can be implemented either with a loop or a tail recursive function
  • the switch is implemented using a dictionary and function calls or multiple if...elif...else.
By the way, translating literally from one language to another is a very bad idea.
and the python code is ... ?
 
and the python code is ... ?
where is the code to translate?

you can do a while loop like:
Code:
...
while True:
  # do something
  if condition: break
or
Code:
...
def tailrec(*args, **kwargs):
  """Tail recursive method"""
  if cond: return
  #do stuff
  tailrec(...)

For the switch:
Code:
...
switch_dict = {  key1: func1, key2: func2, key3: func3 }
switch_dict[value](*args, **kwargs)
or
Code:
...
switch_result = {
  cond1: func1,
  cond2: func2,
  cond3: func3
}[1]
or
Code:
...
if cond1:
  ...
elif cond2:
  ...
elif cond3:
  ...
else:
  ...

Although you can also have ranges in your conditions and
 
switch(exp){
case i_th: // code ; break;
}

is usually used when several branches are required, (more than 2).

My C code uses switch statement for +10

case i_th: // code; break;

how to code switch() C-instruction in the most simmilar way in python ?
 
switch(exp){
case i_th: // code ; break;
}

is usually used when several branches are required, (more than 2).

My C code uses switch statement for +10

case i_th: // code; break;

how to code switch() C-instruction in the most simmilar way in python ?

Why are you asking? Obviously Python doesn't do everything C does exactly in the same way, or vice versa.
 
Thanks for your reply.
what about

do{
//...
}while();


?

it differs from while(){}
it's the same thing, for and do...while are syntactic sugar around a while loop. @pingu made it clear before. I don't get your point.
 
Back
Top