简体   繁体   中英

How is "class.object.function" implemented in python?

So in Pandas we can do str operations on a string column like

str_lower = df["str_col"].str.lower()

I wonder, how is the str.lower() implemented in a class (note it is not about the specific implementation of str.lower() but more how such a thing would be implemented in python generally)?

The only thing I can think of, Is a method of a sub-class defined in the class eg

class DataFrame():
     class str():
           def lower(self):
                 return [p.lower() for p in self.column]

but I doubt it's correct

Since Pandas is open source, you can find the code on Github! Here is the .str implementation page. The _map_and_wrap function provides a nice way of understanding what's happening, start with it and go deeper!

def _map_and_wrap(name, docstring):
    @forbid_nonstring_types(["bytes"], name=name)
    def wrapper(self):
        result = getattr(self._data.array, f"_str_{name}")()
        return self._wrap_result(result)

    wrapper.__doc__ = docstring
    return wrapper

Maybe it uses property , it allows you to get the return value of a function like a attribute:

>>> class Foo:
...     def __init__(self, *args):
...         self.lst = list(args)
...     @property
...     def string(self):
...         return ' '.join(map(str, self.lst))
...
>>> f = Foo(1, 2, 3, 4, 5)
>>> f.string
'1 2 3 4 5'
>>> f.string.split()
['1', '2', '3', '4', '5']

The essence of property is to wrap a function into a descriptor . You can consider learning about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM