How Best to Initialize a Class Using an Attribute from Attrs?
In Vanilla Python to Create a Class Which, When Initiated, Would Also Initiate Another Class I Would Use __Init__(Self): Class Someclass: __Init__(Self, Token...
In vanilla Python to create a class which, when initiated, would also initiate another class I would use __init__(self):
class SomeClass:
__init__(self, token: str):
self.token = token
client = WebClient(auth=self.token)
But in attrs, I can't do this. The following yields an error because it is passing a _CountringAttr object, not the resolved string.
@attr.s()
class SomeClass:
token: str = attr.ib()
client = WebClient(auth=token)
What is the "right" way of accomplishing this?
1 Answer
You can either use a default:
@attr.define
class SomeClass:
token: str
client: WebClient = attr.field() # needed! attr.ib works too
@client.default
def _client_factory(self):
return WebClient(self.token)
or __attrs_post_init__:
@attr.define
class SomeClass:
token: str
client: WebClient = attr.field(init=False)
def __attrs_post_init__(self):
self.client = WebClient(self.token)
P.S. I'm using the modern attrs APIs to make them more known. Check out if you want to know more.