Scavenger23
Scavenger23

Reputation: 209

How to tell Python to start executing code from a given line

I am completely new to Python, just learning strings and variables at the moment. I know you can put a "#" before code to comment it, but is there an option for Python to completely ignore part of the code and start working from the lines after this code?

For example:

old_price = 30
new_price = 25
difference = old_price - new_price

name = "John Smith"
age = 15
print(name, age)

I would like the code to start working from the line name="John Smith", without having to comment the first 3 lines.

Upvotes: 2

Views: 928

Answers (4)

YAYAdest
YAYAdest

Reputation: 118

Python is a structured programming language and therefore this type of things is discouraged and not supported internally (just like goto instructions). Moreover, usually the first lines of code would be import statements that would be needed later, so you don't really want to skip them.

You SHOULD use the benefits of a structured language and put your code into functions, then you can decide later if you want to execute those functions. Another solution is to use comments (or block comments) for code that you don't want to execute currently, or for other textual lines that don't contain code.


However, just for fun, here are two ways to skip lines.

Using -x flag

You can run your program with -x flag: python -x <python_file>. This will only skip the first line. This flag is used for allowing non-Unix forms of #!cmd (as describing when running python -h).

Using another script to run your script

Python has no internal support to run from a given line, so let's cheat. We can create a script file that its only purpose is to execute another file from a given line. In this example I implemented three ways to do so (note that the exec command is bad and you should not really use that).

import os
import subprocess
import sys
import tempfile

skip_to = int(sys.argv[1])
file_path = sys.argv[2]

with open(file_path, 'r') as sourcefile:
    new_source = '\n'.join(sourcefile.readlines()[skip_to:])

    # Several ways to execute:

    # 1. Create tempfile and use subprocess
    temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    temp.write(new_source)
    temp.close()
    subprocess.call(['python', temp.name] + sys.argv[2:])
    os.remove(temp.name)

    # 2. Run from memory using subprocess and python -c
    subprocess.call(['python', '-c', new_source] + sys.argv[2:])

    # 3. Using exec (usually a very bad idea...)
    exec(new_source)

An example code to skip:

print("first line")
print("second line")
print("third line")
from datetime import datetime

print(datetime.now())
>>python skip.py 3 main.py
>>2019-12-05 20:50:50.759259
>>2019-12-05 20:50:50.795159
>>2019-12-05 20:50:50.800118

Upvotes: 0

Diego Palacios
Diego Palacios

Reputation: 1144

You can use multiline strings to comment the whole block, not having to put # in each line:

"""
old_price = 30
new_price = 25
difference = old_price - new_price
"""
name = "John Smith"
age = 15
print(name, age)

Upvotes: 8

sedavidw
sedavidw

Reputation: 11691

I don't believe that there is and I don't think that it's actually a good idea to do so. Why would you have code you don't want to execute?

If you don't want to execute it YET, you can put code into a function and not call it.

If you're just experimenting and want to play around around you should consider things like IPython or Jupyter Notebook where you can execute code interactively

Upvotes: 0

sławomir sowa
sławomir sowa

Reputation: 70

You could define a function an call it:

# define function old()
def old():
    old_price = 30
    new_price = 25
    difference = old_price - new_price
    print (difference)

# define function string()
def string():
    name = "John Smith"
    age = 15
    print (name, age) 

string() # out: John Smith 15

Upvotes: 0

Related Questions