1 year ago
#340138
The Vindow Viper
How do I implicitly change an instance attribute by modifying another attribute of which it is comprised?
So I have a class and initiate the instances to have attributes as described:
class Orders():
def __init__(self, country='USA', product='Shoes'):
self.profile = 'https://website.com/user'
self.token = 'leHAiOjE2Nz'
self.country = country
self.product = product
self.order_id = ''
self.check_order = 'https://website.com/user/orders/' + self.order_id
When I then change the value of order_id, self.check_order is still assigned to the string + original order_id.
session1 = Orders()
print(id(session1.check), f'{session1.check=}')
print(id(session1.order_id), f'{session1.order_id=}')
session1.order_id = '123333079'
print(id(session1.order_id), f'new {session1.order_id=}')
print(id(session1.check), f'{session1.check=}')
yields
1726088546304 session1.check='https://website.com/user/orders/'
1725654976112 session1.order_id=''
1725660870256 new session1.order_id='123333079'
1726088546304 session1.check='https://website.com/user/orders/'
As you can see, session1.check is the same.
The only way I've found to modify it is with an explicit reassignment:
session1.check = session1.check+session1.order_id
However, this is not desirable as I would want this change to be implicit due to the nature of the method structure and mutations I need to perform on these attributes. I've watched Ned Batchelder's pycon presentation on names vs. values but I am stumped on how to make this work implicitly. There must be an elegant solution.
python
attributes
instance
implicit
mutated
0 Answers
Your Answer