繁体   English   中英

Python:扩展 class 方法并在 class 的实例中使用替换

[英]Python: Extend class method and use the substituted in instance of the class

我想为我自己的项目需要扩展 pyspark.sql.ZBA834BA059A9A379459C112E7 的读/写 function。 为此,我创建了以下内容

import pyspark.sql

class DataFrame(pyspark.sql.DataFrame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def write(self, format="parquet", version=True):
        if format=="some_format":
            # do something
            super().write.format(format).save(path)

The trouble is that in the code we instantiate the super class object like this data = spark.range(0, 5) How do I go about converting a spark Dataframe to have my custom read/write method with minimal changes. 这可能吗

您可以像这样更改现有 object 的 class :

data = spark.range(0, 5)
data.__class__ = DataFrame   # _your_ DataFrame

对于父 class 的直接扩展,这应该可以正常工作。 一般来说,像这样入侵 class 成员有各种注意事项 例如,您自己的初始化程序尚未在此变量上调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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