Set Attribute Conversion Property

An assignment statement where the target is a "get attribute" operation can be converted to a "set attribute" operation as follows:

getattr(target_1, target_2) = value = setattr(target_1, target_2, value)

The first two arguments of the "set attribute" function call is exactly the same as the "get attribute" call. The "set attribute" function also takes a third argument which is the value that we want to apply. For example:

self.first_name = first_name = self."first_name" = first_name

Here, we want to update "self.first_name" to "first_name". Try stepping through the simulator to see how LW Python executes the assignment statement.

Code Editor
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def age_in_months(self):
return __mul__(self.age, 12)
p = Person("John", "Smith", 25)
age = p.age_in_months()
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments