簡體   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