0

It's just a curiosity about python. Is there a way to write anything in python files without getting any error and without using comments? It could be a macro / pre processor word / python option to ignore the lines.
For instance:

#!/usr/bin/python
# coding: utf-8

I am writing anything I want here!


def func1(number):
   print(number)

func1(3)

and the result wouldn't trigger any error, printing the number 3.

A similar C++ question: Force the compiler to ignore some lines in the program

Community
  • 1
  • 1
almanegra
  • 653
  • 8
  • 21
  • As a comment? Preface the lines with "#". – Larry Lustig Jun 25 '14 at 18:29
  • sorry, I just edited the question: I mean, without comments! – almanegra Jun 25 '14 at 18:31
  • That's why I'm asking. I want to know if it's possible (somehow) supress / ignore the error using a trick/macro/code/script or something. It's just curiosity anyway. – almanegra Jun 25 '14 at 18:34
  • @almanegra when you want to surpress/ignore the error whats the purpose to have battle with it at first place, why not use comment? – Aamir Rind Jun 25 '14 at 18:36
  • @aamir-adnan I just want to learn new features. Look at the link I just attached to the question. – almanegra Jun 25 '14 at 18:43
  • The answers to that question state that you *have to indicate to the compiler* which lines should be ignored and under what conditions. You can indicate that to the Python compiler by commenting out the lines. – Larry Lustig Jun 25 '14 at 18:46
  • I just found an answer. The solution is called conditional compilation – almanegra Jun 25 '14 at 18:53
  • Python does not offer conditional compilation and if it did it would *still* require you to specify which lines to include and which not to include. Conditional compilation will *not* automatically detect "normal text" and "Python text". In Python, the closest you'll get is using a regular old `if` statement, possibly placing your function `def`s inside the conditional part of the statement. – Larry Lustig Jun 25 '14 at 19:12
  • Yes, thats right. Although I was trying to come as close as possible to the solution :) – almanegra Jun 25 '14 at 19:25

4 Answers4

0

You could use comments and possibly add a special character outside of the comments if your goal is to apply a custom pre-processor. This would be similar to what the #! commands at the top of your file.

For example, in the following, I just used M: as the special prefix:

#!/usr/bin/python
# coding: utf-8

# M: I am writing anything I want here!


def func1(number):
   print(number)

func1(3)
R. Barzell
  • 666
  • 5
  • 24
0

Without comments:

Without comments, you could surround with quotes and assign to a variable:

tmp = "I am writing anything I want here!"

As such:

#!/usr/bin/python
# coding: utf-8

tmp = "I am writing anything I want here!"


def func1(number):
   print(number)

func1(3)

With comments

You can comment it out as such:

1. Multi-line comments/strings

#!/usr/bin/python
# coding: utf-8

"""I am writing anything I want here!
Yep, literally anything!

def func1(number):
   print(number)

func1(3)

2. Single-line comments

#!/usr/bin/python
# coding: utf-8

#I am writing anything I want here!


def func1(number):
   print(number)

func1(3)
0

The easiest way to do this, although it's technically not Pythonic and should not be left in the final draft as it creates an unused string, is with triple quotes on each side. It's the simplest way to toggle medium-large portions of code. Something like:

x = 5

""" DEBUG:
x += 5
x *= 5 * 5
x = x % 3
x -= 2
"""

print x

If it's one or two lines, you can always use normal comments,

#like this

For your code, you'd want:

#!/usr/bin/python
# coding: utf-8
"""
I am writing anything I want here!
"""

def func1(number):
  print(number)

func1(3)
Cameron
  • 127
  • 10
0

I just found something that I was looking for.

The aswer to my question is available here:

Conditional compilation in Python

https://code.google.com/p/pypreprocessor/

Community
  • 1
  • 1
almanegra
  • 653
  • 8
  • 21