1

I need to define a variable of a class a class object. How can I do it?

if for example I have a class like this :

class A:
  def __init__(self, a, b):
     self.a = a
     self.b = b

and I want to create another class B that have a variable as instance of class A like :

class B:
  def __init__(self, c = A(), d):
     self.c = c
     self.d = d 

How can I do it ? I need to do particular operation or simply declarate c as object of class A when I create the object of class B ?

3
  • 2
    Do it in the function: self.c = A(). Default values are evaluated when the function is defined, not when it's called. Commented Jul 5, 2022 at 15:06
  • 2
    The code you have will create a single instance of A, and use that same instance as the default for every instance of B that you create. Commented Jul 5, 2022 at 15:06
  • Hello and welcome to StackOverflow! Are you wanting to have an instance of A as a default argument to B.__init__? Commented Jul 5, 2022 at 15:11

2 Answers 2

2
class B:
  def __init__(self, a, b, d):
     self.c = A(a, b)
     self.d = d

or

class B:
  def __init__(self, c, d):
     self.c = c
     self.d = d

or

class B:
  def __init__(self, d):
     self.c = A(a, b)      # a and b can be values
     self.d = d
Sign up to request clarification or add additional context in comments.

1 Comment

Might need a little more explanation, especially on the last one (maybe provide some actual literals in there instead of using a and b which won't work since they haven't been defined). The critical thing is that you can't just say A() because A.__init__ requires two arguments.
1

What you wrote mostly works:

    def __init__(self, c = A(), d):
        self.c = c

But there's a "gotcha" which you really want to avoid. The A constructor will be evaluated just once, at def time, rather than each time you construct a new B object. That's typically not what a novice coder wants. That signature mentions a mutable default arg, something it's usually best to avoid, if only to save future maintainers from doing some frustrating debugging.

https://dollardhingra.com/blog/python-mutable-default-arguments/

https://towardsdatascience.com/python-pitfall-mutable-default-arguments-9385e8265422

Instead, phrase it this way:

class B:
    def __init__(self, c = None, d):
        self.c = A(1, 2) if c is None else c
        ...

That way the A constructor will be evaluated afresh each time. (Also, it would be good to supply both of A's mandatory arguments.)

3 Comments

"That's not what you want" - I don't think you can say that for sure, it's really not clear from the question what the intention is.
A() will raise an error because it's missing the a and b arguments.
I would prefer if c is not None: self.c = c to self.c = c or .... If c can only be some class instance, which is normally True in boolean context, this will work ok, but more generally, it would prevent passing anything falsy for c, like 0, an empty string or list...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.