[英]Python: Extend class method and use the substituted in instance of the class
I want to extend the read/write function of pyspark.sql.DataFrame for my own project needs.我想为我自己的项目需要扩展 pyspark.sql.ZBA834BA059A9A379459C112E7 的读/写 function。 To that end I create the following
为此,我创建了以下内容
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. 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. Is this possible这可能吗
You can change the class of an existing object like this:您可以像这样更改现有 object 的 class :
data = spark.range(0, 5)
data.__class__ = DataFrame # _your_ DataFrame
For straightforward extensions of the parent class, this ought to work fine.对于父 class 的直接扩展,这应该可以正常工作。 In general there are all sorts of caveats to hacking class membership like this;
一般来说,像这样入侵 class 成员有各种注意事项; for example, your own initializer has not been called on this variable.
例如,您自己的初始化程序尚未在此变量上调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.