Indent and comments in function in Python

2024/11/16 15:26:30

I am using Python 2.7 and wrote the following:

def arithmetic(A):x=1
"""
Some comments here
"""  if x=1:x=1elif x=2:x=2return 0

But it has the indentation issue:

if x=1:^
IndentationError: unexpected indent

So how to write comments in the function?

Answer

The """ xxx """ is a docstring. Yes, it can be used as a comment, but it winds up being part of the actual code, so it needs to be indented:

def arithmetic(A):x=1"""Some comments here"""  if x==1:x=1elif x==2:x=2return 0

If you use line-oriented comments starting with #, those are not part of the actual code, so their indentation doesn't matter.

One nice thing about docstrings is that tools can use them, for example, to display information about functions. If you've ever used help(some_function) at the Python command prompt, you have seen a docstring.

In fact, if you load your function into an IDE, and then type help(arithmetic), you can see "Some comments here".

I modified your code slightly, because in Python, = is for assignment, and you must use == in your if statement to check for equality.

So the code will compile and run as-is, but note that only setting x to 1 if x is already equal to 1 won't actually do anything :)

https://en.xdnf.cn/q/71333.html

Related Q&A

Read a large big-endian binary file

I have a very large big-endian binary file. I know how many numbers in this file. I found a solution how to read big-endian file using struct and it works perfect if file is small:data = []file = open(…

SWIG Python Structure Array

Ive been searching for a few days trying to figure out how to turn an array of structures into a Python list. I have a function that returns a pointer to the beginning of the array.struct foo {int memb…

Hashing tuple in Python causing different results in different systems

I was practicing tuple hashing. In there I was working on Python 2.7. Below is the code:num = int(raw_input()) num_list = [int(x) for x in raw_input().split()] print(hash(tuple(num_list)))The above cod…

ctypes pointer into the middle of a numpy array

I know how to get a ctypes pointer to the beginning of a numpy array:a = np.arange(10000, dtype=np.double) p = a.ctypes.data_as(POINTER(c_double)) p.contents c_double(0.0)however, I need to pass the po…

Extracting unsigned char from array of numpy.uint8

I have code to extract a numeric value from a python sequence, and it works well in most cases, but not for a numpy array.When I try to extract an unsigned char, I do the followingunsigned char val = b…

How to hold keys down with pynput?

Im using pynput and I would like to be able to hold keys down, specifically wasd but when I try and run this code it only presses the key and doesnt hold it for 2 seconds. If anyone knows what Im doing…

How well does your language support unicode in practice?

Im looking into new languages, kind of craving for one where I no longer need to worry about charset problems amongst inordinate amounts of other niggles I have with PHP for a new project.I tend to fin…

analogy to scipy.interpolate.griddata?

I want to interpolate a given 3D point cloud:I had a look at scipy.interpolate.griddata and the result is exactly what I need, but as I understand, I need to input "griddata" which means some…

Mongodb TTL expires documents early

I am trying insert a document into a Mongo database and have it automatically expire itself after a predetermine time. So far, my document get inserted but always get deleted from the database from 0 -…

sqlalchemy, hybrid property case statement

This is the query Im trying to produce through sqlalchemySELECT "order".id AS "id", "order".created_at AS "created_at", "order".updated_at AS "u…