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

9
  • As a comment? Preface the lines with "#". Commented Jun 25, 2014 at 18:29
  • sorry, I just edited the question: I mean, without comments! Commented Jun 25, 2014 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. Commented Jun 25, 2014 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? Commented Jun 25, 2014 at 18:36
  • @aamir-adnan I just want to learn new features. Look at the link I just attached to the question. Commented Jun 25, 2014 at 18:43

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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)

Comments

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/

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.