I am trying to figure the best way to connect classes. Specifically, I want to know whether it is Pythonic to reference an object when creating a class.
So here are two examples that produce the same outcome:
Example 1
class A:
def __init__(self,content):
self.content = content
class B:
def __init__(self):
self.content = a.content
a = A('test')
b = B()
print(b.content)
Example 2
class A:
def __init__(self,content):
self.content = content
class B:
def __init__(self,other_object):
self.content = other_object.content
a = A('test')
b = B(a)
print(b.content)
In example 1 the object a is being used inside of the class. In example 2 that object is passed in as argument.
I get that example 2 is the better option because it is more deliberate, but would example 1 still be good practice?
Aand under the global namea? If not, go for Example2.