python - Pandas 源代码 _get_grouper 调用自己
问题描述
我在看下面的pandas源码:
def _get_grouper(self, obj, validate=True):
"""
Parameters
----------
obj : the subject object
validate : boolean, default True
if True, validate the grouper
Returns
-------
a tuple of binner, grouper, obj (possibly sorted)
"""
self._set_grouper(obj)
self.grouper, exclusions, self.obj = _get_grouper(self.obj, [self.key],
axis=self.axis,
level=self.level,
sort=self.sort,
validate=validate)
return self.binner, self.grouper, self.obj
看起来 _get_grouper 递归调用自己。这不会导致无限循环吗?
我试图搜索父类,但似乎Grouper类只继承了对象类,文件中没有定义其他_get_grouper函数。
这让我有点困惑。
解决方案
请注意,在类之外还有另一个_get_grouper
函数,正是在此代码段中调用的函数。
如果它在_get_grouper
被调用的类中是相同的,则应该将其self._get_grouper
指定为该类的属性。
这是一个简单的例子来说明这一点:
class Sample():
def __init__(self,p):
self.p = p
if self.p:
print_()
else:
self.print_()
def print_(self):
print('This is a function within the Sample class')
def print_():
print('This is a function outside the Sample class')
s = Sample(p=True)
# This is a function outside the Sample class
s = Sample(p=False)
# This is a function within the Sample class
推荐阅读
- gmail - gmail说用户超过配额,尽管用户有很多空间
- rule-engine - 创建规则引擎 Drools 时出错
- c++ - 如何将训练数据从 UCI 加载到 OpenCV?
- java - Java 进程 - 等待进程退出
- ember.js - 当用户取消编辑操作时,ember 回滚属性
- opengl - 为两个不同的 VAOS 使用两个不同的着色器
- python - 风玫瑰蟒的自定义缩放
- php - 致命错误:未捕获的错误:在第 9 行的布尔值上调用成员函数 execute()
- html - table-layout:fixed 是否应该允许我指定单元格大小加起来比我的容器更大?
- javascript - DotNetBrowser 调用 javascript 函数到 c#