0

I have the following project structure:

main.py
utils
----hudi.py
project
----stage.py

The main file will create an instance of the class in stage.py, that will create an instance of the hudi utils class. What I want is to set some attributes of Stage class as default values of the Hudi class.

The code of hudi.py is

class Hudi:
    def __init__(self, spark):
        self.spark = spark

    def test_hudi(self):
        print(self.spark)

My stage.py code is:

from utils.hudi import Hudi

class Stage:
    def __init__(self):
          self.spark="spark"
    def a(self):
          hudi = Hudi(spark=self.spark)
          hudi.test_hudi()

Reading the answer https://stackoverflow.com/a/15189304/9621172 I have tried with the following code:

class Hudi:
    def __init__(self, spark=None):
        self.spark = spark or self.spark

    def test_hudi(self):
        print(self.spark)


from utils.hudi import Hudi

class Stage:
    def __init__(self):
          self.spark="spark"
    def a(self):
          hudi = Hudi()
          hudi.test_hudi()          

This way I expected that self.spark will be "transfered" to Hudi class without having to explicitly pass it (let's say that I want Hudi.spark = Stage.spark), but I get the error AttributeError: 'Hudi' object has no attribute 'spark'. What I want is even possible?

Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41
  • 1
    you cannot do `self.spark = spark or self.spark`. if the parameter `spark` evaluates false you will be doing `self.spark = self.spark` but `self.spark` is not yet defined –  Apr 04 '22 at 08:24

1 Answers1

0

Will static variables be what you want? Static class variables and methods in Python stage.py

from utils.hudi import Hudi

class Stage:
    def __init__(self):
          self.spark="spark"

    def SetHudi(self, spark):
          Hudi.spark = self.spark
          testHudi()
def testHudi():
    print(Hudi.spark)

class Hudi:
    spark = ""
    def __init__(self):
        pass
Joe_Bao
  • 116
  • 6