Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Micropython interpreter differences between cmd line and program

    Scheduled Pinned Locked Moved MicroPython
    3 Posts 2 Posters 40 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      kjm
      last edited by kjm

      When writing code I sometimes try out syntax I'm unsure of at the command line & if it works I put it in the program I'm working on. My observation is that the interpreter parsing is somewhat forgiving, if unpredictably so, at the command line but less so with programs, particularly if the syntax is not strictly legal.

      For example

      >>> for i in range (2): print('i is 1') if i else 'i is zero'
      ... 
      'i is zero'
      i is 1
      

      or

      >>> for i in range (2): print('i is 1') if i else ('i is zero')
      ... 
      'i is zero'
      i is 1
      

      I have no idea why this instruction runs or why the quotes are displayed for the first print not the other. If I try the same instruction as a one line program

      >>> Running test.py
      
      >>> 
      >>> 
      i is 1
      

      the results are different. If it's the same interpreter doing the parsing, why the different response to a command line instruction & the same instruction in a program?

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @kjm
        last edited by robert-hh

        @kjm REPL always prints the result of the expression which it gets as input. In this case, it's the string. Since it executes code, it also prints the result from the code execution. When you import that code snippet, the expression results are not printed.
        For instance, if you just take the statement:
        1
        If you enter that in REPL, it will evalute that and respond with the result:
        1
        If you put that single statement into a script an run it, nothing is printed.
        That explains the difference. So if you look at your code, for i=0 the expression 'i is zero' is executed, for i=1 the expression print('i is 1'). That results in different text being shown. Besides that, didn't you intend to have a different statement:

        for i in range (2): print('i is 1' if i else 'i is zero')
        

        which will print:

        i is zero
        i is 1
        

        after the second enter closing the for loop.

        1 Reply Last reply Reply Quote 2
        • K Offline
          kjm
          last edited by

          @robert-hh said in Micropython interpreter differences between cmd line and program:

          for i in range (2): print('i is 1' if i else 'i is zero')

          The if else inside (the print brackets) I should have known, to much time inside python 2.7, that's my problem. Tnx Rob.

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom