Reputation: 11
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
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
Upvotes: 1
Views: 402
Reputation: 18806
.
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)
Upvotes: 1