1

I'm learning python and here's one code I can't quite get:

text = 'zip is very zipped'
print text.find('zip', text.find('zip') + 1)

Now, I know this is a shortcut of accomplishing:

text = 'zip is very zipped'
occur_once = text.find('zip')
print text.find('zip', occur_once + 1)

I was wondering, how the does

print text.find('zip', text.find('zip') + 1)

works and in what order does Python evaluates these expressions? Is there a name for this kind of 'order' of execution?

3 Answers 3

4

Python uses strict (eager) evaluation strategy: the arguments to a function are always evaluated completely before the function is applied. The evaluation order is left to right (except when evaluating assignment):

Not descending into attribute lookups:

  1. Evaluate text.find (we'll name the result F1)
  2. Evaluate 'zip' → A1
  3. Evaluate text.find → F2
  4. Evaluate 'zip' → A2
  5. Call F2.__call__(A2) (we'll call the return value R1) (text.find('zip'))
  6. Evaluate 1 → A3
  7. Call R1.__add__(A3) (returns R2) (R1 + 1)
  8. Call F1.__call__(A1, R2) (returns the final result) (text.find('zip', R2))
Sign up to request clarification or add additional context in comments.

4 Comments

I think 5 actually happens after 6, dis.dis seems to agree with me.
Makes sense, arguments to __add__ should be fully evaluated left to right, thanks.
What's those ___ variables? I'm beginning in python and can't really understand what those mean.
@daremarkovic, those are special methods that python uses to add and call objects. You don't need to worry about them unless you are creating your own objects that can be added or called.
2

Python will evaluate the arguments left to right in a function call, then the function itself is executed.

General evaluation order is left to right.

5 Comments

I'm pretty sure the function value (not it's return value, but the function itself) is evaluated before the arguments. This only matters if one of the arguments is a function call with side effects (that change the function definition), which is pretty unlikely to happen in code not designed specifically to test this.
@Blckknght Ah, I'll clarify what I meant, which was that the function is then executed. It is indeed true that the callable will be evaluated before the argument list.
Can you give an example of how that would apply to this: print text.find(‘zip’,text.find(‘zip’)+1)
@daremarkovic It would evaluate the leftmost arguments first, then proceed to the right. I don't really know how to make it simpler than that.
Can you tell me in this specific example: print text.find(‘zip’,text.find(‘zip’)+1) how it would evaluate them (which ones first)?
1

It is called operator precedence and evaluation order. Within an expression, operator precedence applies, and per expression the evaluation order is used.

The text.find() call comes before the + addition operator because it has a higher precedence.

For operators of equal priority, evaluation goes from left to right. In a function call, each argument is a separate expression and these are thus evaluated from left to right.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.