【问题标题】:Splitting class functions across multiple cells in Jupyter?在 Jupyter 中跨多个单元格拆分类函数?
【发布时间】:2021-02-12 16:14:54
【问题描述】:

给定以下代码:

class DTC:
    def __init__(self):
        self.__root = None

    def unique(self,Y):
        d = {}
        for i in Y:
            if i not in d:
                d[i]=1
            else:
                d[i]+=1
        return d

    def ent(self,Y):
        freq = self.__count_unique(Y)
        ent_ = 0
        total = len(Y)
        for i in freq:
            p = freq[i]/total
            entropy_ += (-p)*math.log2(p)
        return ent_

如果将其放置在 Jupyter Notebook 的单个单元格中,上述内容将运行。但是,如果我希望类代码像这样拆分为多个单元格,如何使类代码正常工作:

单元格 1

class DTC:
    def __init__(self):
        self.__root = None

单元格 2

    def unique(self,Y):
        d = {}
        for i in Y:
            if i not in d:
                d[i]=1
            else:
                d[i]+=1
        return d

细胞 3

    def ent(self,Y):
        freq = self.__unique(Y)
        ent_ = 0
        total = len(Y)
        for i in freq:
            p = freq[i]/total
            ent_ += (-p)*math.log2(p)
        return ent_

【问题讨论】:

  • "是否可以从 Jupyter 中的类中拆分函数?" - 你可以测试一下,真的不需要太多时间
  • 对不起,我原来的问题是措辞不正确。编辑了帖子。是的,我之前已经尝试过了。
  • 有两种方法可以实现!

标签: python-3.x oop jupyter-notebook jupyter


【解决方案1】:

在 Jupyter Notebooks 中有两种方法可以将类定义拆分到多个单元格

方法一

以天真的方式来做(利用继承和覆盖):

细胞 1

class DTC:
    def __init__(self):
        self.__root = None

细胞 2

class DTC(DTC):
    def unique(self,Y):
        d = {}
        for i in Y:
            if i not in d:
                d[i]=1
            else:
                d[i]+=1
        return d

细胞 3

class DTC(DTC):
    def ent(self,Y):
        freq = self.__count_unique(Y)
        ent_ = 0
        total = len(Y)
        for i in freq:
            p = freq[i]/total
            entropy_ += (-p)*math.log2(p)
        return ent_

需要注意的是,这实际上在内部创建了一个类的层次结构:

import inspect
inspect.getmro(DTC)
# outputs: (__main__.DTC, __main__.DTC, __main__.DTC, object)

如果您不打算拉伸过多的单元格,则可以使用此方法。

方法二

使用包jdcjdc 的更多详细信息/文档

单元格 1

import jdc        # jupyter dynamic classes

class DTC:
    def __init__(self):
        self.__root = None

细胞 2

%%add_to DTC
def unique(self,Y):
    d = {}
    for i in Y:
        if i not in d:
            d[i]=1
        else:
            d[i]+=1
    return d

细胞 3

%%add_to DTC
def ent(self,Y):
    freq = self.__count_unique(Y)
    ent_ = 0
    total = len(Y)
    for i in freq:
        p = freq[i]/total
        entropy_ += (-p)*math.log2(p)
    return ent_

这次没有形成层次结构:

import inspect
inspect.getmro(DTC)
#output: (__main__.DTC, object)

【讨论】:

【解决方案2】:

只有 python 的解决方案:

class OutsourceMethods:
    @classmethod
    def method(cls, f):
        setattr(cls, f.__name__, f)
        

用作:

class A(SuperA, OutsourceMethods):
    def __init__(self):
        self.x = 10


@A.method
def bar(self, y):
    print(self.x, y)

a = A()

a.bar(20)

> 10 20

b = A()
b.x = 3
b.bar() 

> 3 20

这不是 100% 等价的,但到目前为止我还没有注意到有什么不同。

【讨论】:

    猜你喜欢
    • 2013-01-14
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多