1

i'm a beginner of python and i have a question. (I've been curious about that really really)

when we use pandas in python, we can use function like below

example)

import pandas as pd
data = {'name':['Joe','Miachel'], 'age':[20,30]}
df = pd.DataFrame(data)    

to get information, we can use functions

  1. df.info()
  2. df.info

we can get results from number 1 and 2 both

not only info, but also describe(), describe and etc... sometimes i have to use () at the end of code to get a result, but sometimes i don't have to use it

what is different? please teach me about that clearly thank you

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
이영훈
  • 11
  • 2
  • 2
    Does this answer your question? [python pandas functions with and without parentheses](https://stackoverflow.com/questions/27980843/python-pandas-functions-with-and-without-parentheses) – Brian61354270 Nov 16 '21 at 00:42
  • 1
    If `info` is just a piece of information (or a property), you use `df.info`. If `info` is a function, you use `df.info()`. It's just like any other Python variable. You have to know this, from the documentation. – Tim Roberts Nov 16 '21 at 00:42
  • 2
    `df.info` doesn't actually give you the info... It says ``, where `...` stands for the `df` repr. – wjandrea Nov 16 '21 at 00:44
  • 1
    "we can get results from number 1 and 2 both" no, no you cannot. – juanpa.arrivillaga Nov 16 '21 at 01:04

1 Answers1

1

. names an attribute of an object, while () calls it

Some methods can be represented and have a similar function to being called a filled __repr__, which shows you what they are

In the case of .info, it's both callable and representable, providing similar information each time

Simply refer to the docs or explore for an instance of any particular object!

You can use dir() to list the methods on an object or help() to get generated info (potentially quite detailed)

>>> o = object()
>>> o
<object object at 0x10d73efb0>
>>> o()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'object' object is not callable

Here's an example with 3 very simple classes (classes inherit from object if they don't specify some base class)

class Foo():
    pass

class Bar():
    def __repr__(self):
        return "called __repr__ on an instance of Bar"

class Baz():
    def __call__(self):
        return "called an instance of Baz"

Now we can see how they behave when represented or called!

>>> a = Foo()
>>> b = Bar()
>>> c = Baz()
>>> a
<__main__.Foo object at 0x113d1ff40>
>>> b
called __repr__ on an instance of Bar
>>> c
<__main__.Baz object at 0x1041f7a30>
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Foo' object is not callable
>>> b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Bar' object is not callable
>>> c()
'called an instance of Baz'
>>> dir(c)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

help(c)

Help on Baz in module __main__ object:

class Baz(builtins.object)
 |  Methods defined here:
 |
 |  __call__(self)
 |      Call self as a function.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
ti7
  • 16,375
  • 6
  • 40
  • 68
  • As I have trouble with it and the line is fuzzier in Python, more on methods vs attributes https://stackoverflow.com/questions/46312470/difference-between-methods-and-attributes-in-python – ti7 Nov 16 '21 at 00:53
  • 2
    *"In the case of `.info`, it's both callable and representable, providing similar information each time"* -- That's not correct; OP is mistaken. – wjandrea Nov 16 '21 at 00:54
  • 1
    Many methods of Pandas DataFrames are representable with information about the DataFrame (in this case, its contents in two different views) .. displaying it does _not_ call it, however, which should be clear, or more specifically, the representation of `.info` is _not_ the same as it being called! (though it could be, this would also be somewhat confusing) – ti7 Nov 16 '21 at 00:56
  • I really appreciate your answer!!! – 이영훈 Nov 16 '21 at 04:43
  • @이영훈 anytime! If this or other Answers solve your Questions, please mark them as the Answer with the check to its left to help yourself and the community! – ti7 Nov 16 '21 at 15:50