Search code examples
pythonapache-sparkpysparkmethodspython-class

what is attribute method in python


I was learning spark and came across this line

Your SparkSession has an attribute called catalog which lists all the data inside the cluster. This attribute has a few methods for extracting different pieces of information. One of the most useful is the .listTables() method, which returns the names of al the tables in your cluster as a list e.g. spark.catalog.listTables().

My question is how an attribute can have methods in python. I tried to search a lot but could not find anything which could clear my confusion. I do know that class has attributes and methods and how to access them but I am confused that how can attribute(catalog) has methods(listTables()) and the way method is being called like catalog.listTables(). Can someone please share some examples or links. Thank you.


Solution

  • Everything is an object in python, and can therefore have a method. Here's an example:

    >>> class Test:
    ...     def __init__(self):
    ...         self.a = 3
    ...         self.b = 'abc'
    ...
    >>> t = Test()
    >>> t.a.bit_length()
    2
    >>> t.b.islower()
    True
    

    As you can see, both attributes of t have methods.

    In the case of the attribute in the question, the object is some custom type that has the methods being described.