子类带有额外参数的Python类继承

huangapple go评论107阅读模式
英文:

child class with extra arguments python class inheritance

问题

  1. 我编写了一个父类
  2. ```python
  3. class Parent():
  4. def __init__(self, spark_session=None):
  5. try:
  6. # 实例化 Spark 会话
  7. self.spark = spark_session
  8. if not self.spark:
  9. self.spark = SparkSession.builder.config("spark.sql.debug.maxToStringFields", 1000).appName("SparkTest") \
  10. .getOrCreate()
  11. except Exception as e:
  12. print("初始化 Spark 和日志记录失败: {}".format(e))

然后子类在其构造函数中有一个额外的参数,即路径:

  1. class Child(Parent):
  2. def __init__(self, path, spark=None):
  3. self.spark = super().__init__(spark)
  4. self.path = path

然后当我写下这段代码:

  1. a = Child("path", None)
  2. print(a.spark)

对于:

  1. self.spark = spark_session

我得到了:

  1. AttributeError: 'NoneType' object has no attribute 'spark' None

有任何想要修复这个问题并使 Spark 对象按预期实例化的想法吗?

谢谢。

  1. <details>
  2. <summary>英文:</summary>
  3. I wrote a parent class:
  4. ```python
  5. class Parent():
  6. def __init__(self, spark_session=None):
  7. try:
  8. # Instantiate Spark Session
  9. self.spark = spark_session
  10. if not self.spark:
  11. self.spark = SparkSession.builder.config(&quot;spark.sql.debug.maxToStringFields&quot;, 1000).appName(&quot;SparkTest&quot;) \
  12. .getOrCreate()
  13. except Exception as e:
  14. print(&quot;Initialization of spark &amp; logging couldn&#39;t be performed: {}&quot;.format(e))

Then the child class has an extra argument in its constructor which is path:

  1. class Child(Parent):
  2. def __init__(self, path, spark=None):
  3. self.spark = super().__init__(spark)
  4. self.path = path

Then when I write this:

  1. a = Child(&quot;path&quot;, None)
  2. print(a.spak)

For:

  1. self.spark = spark_session

I get:

  1. AttributeError: &#39;NoneType&#39; object has no attribute &#39;spark&#39; None

Any idea how to fix this and make spark object instantiate as expected.

Regards

答案1

得分: 1

self.spark = super().__init__(spark):

self.spark = super().__init__(spark):
self.spark = super().__init__(spark):

__init__ 不是构造函数。它初始化对象的属性并返回 None,而不是对象本身。

一旦调用 super().__init__self.spark 将自动初始化。

请注意,我还稍微更改了 Parent.__init__。在您的代码中,如果引发异常,self.spark 没有定义,这将导致后续出现其他错误。

  1. class Parent:
  2. def __init__(self, spark_session=None):
  3. self.spark = spark_session
  4. try:
  5. if not self.spark:
  6. self.spark = SparkSession.builder.config("spark.sql.debug.maxToStringFields", 1000).appName("SparkTest") \
  7. .getOrCreate()
  8. except Exception as e:
  9. print("初始化 Spark 和记录无法执行: {}".format(e))
  10. class Child(Parent):
  11. def __init__(self, path, spark=None):
  12. super().__init__(spark)
  13. self.path = path
英文:

self.spark = super().__init__(spark)

__init__ is not a constructor. It initializes the object's attributes and returns None, not the object.

self.spark will be automatically initialized once you call super().__init__.

Note I also changed Parent.__init__ a bit. In your code self.spark is not defined in case an exception is raised which will lead to other errors down the road.

  1. class Parent:
  2. def __init__(self, spark_session=None):
  3. self.spark = spark_session
  4. try:
  5. if not self.spark:
  6. self.spark = SparkSession.builder .config(&quot;spark.sql.debug.maxToStringFields&quot;, 1000).appName(&quot;SparkTest&quot;) \
  7. .getOrCreate()
  8. except Exception as e:
  9. print(&quot;Initialization of spark &amp; logging couldn&#39;t be performed: {}&quot;.format(e))
  10. class Child(Parent):
  11. def __init__(self, path, spark=None):
  12. super().__init__(spark)
  13. self.path = path

huangapple
  • 本文由 发表于 2020年1月6日 19:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定