What distinguishes a command from needing () vs not?

2024/11/18 17:31:35

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not?

For example:

import pandas as pdcol1=['a','b','c','d','e']
col2=[1,2,3,4,5]df=pd.DataFrame(list(zip(col1,col2)),columns=['col1','col2'])df.columns

Returns Index(['col1', 'col2'], dtype='object') as expected. If we use .columns() we get an error.

Other commands it is the opposite:

df.isna()

Returns:

    col1    col2
0   False   False
1   False   False
2   False   False
3   False   False
4   False   False

but df.isna returns:

<bound method DataFrame.isna of   col1  col2
0    a     1
1    b     2
2    c     3
3    d     4
4    e     5>

Which, while not throwing an error, is clearly not what we're looking for.

What's the logic behind which commands use a () and which do not?

I use pandas as an example here, but I think this is relevant to python more generally.

Answer

Because functions need parenthesis for their arguments, while variables do not, that's why it's list.append(<item>) but it's list.items.

If you call a function without the parenthesis like list.append what returns is a description of the function, not a description of what the function does, but a description of what it is.

As for classes, a call to a class with parenthesis initiates an object of that class, while a call to a class without the parenthesis point to the class itself, which means that if you were to execute print(SomeClass) you'd get <class '__main__.SomeClass'> which is a description of what it is, the same kind of response you'd get if you were to call a function without parenthesis.

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

Related Q&A

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…

Python readline() is not reading a line with single space

I am reading a text file using readline(). My file contains below content with a space at second line:!" # $ % & When I print read values using-print("%s\n" % iso5_char)It prints-!&q…

Bisection search [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Using bisection search to determine I have posted other thread but it did not receive answers thus im trying to provide so…

What am i doing wrong with matplotlibs yticks?

My code is as follows (dont ask for the variable names, im german^^):import matplotlib.pyplot as plt import numpy as np strecke = [] zeit = []daten = open("BewegungBeschleunigung.csv")for i i…

Python- How to implement quick sort when middle element is the pivot?

There are many different versions of quickSort that pick pivot in different ways.Always pick the first element or the last element as the pivot Pick a random element as a pivot. Pick median as the pivo…

How to expose a form when a radio button is checked?

Consider the following sample html template,<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y"&g…