Student
Student

Reputation: 719

Using instance attrs in __init__ signature in Python

Is there any possibility to use internal instance attributes in the signature of a class in Python3? Or if not, what would be the pythonic way to achieve the same effect. What I am thinking of is a signature like this:

  1 class C:
  2     def __init__(self, a=self._b):
  3         self._b = 1
  4         self.a = a

so that I can have a default for a based on some internal attribute _b.

Upvotes: 0

Views: 41

Answers (1)

Samwise
Samwise

Reputation: 71574

Specify the default as a class variable, and use that as the default in both places:

class C:
    default_a = 1

    def __init__(self, a=default_a):
        self._b = self.default_a
        self.a = a

Upvotes: 1

Related Questions