I have a question about why the code below executes what it does.
class Account:
def __init__(self, id):
self.id = id
id = 800
id = self.id + id
acc = Account(123)
print(acc.id)
Why would this code print 123 instead of 923? Why does the id not work inside of the class?
You declare the variable in the scope to self.id + id
, when the init function is finished the scope is gone and therefore id
doesn't exist anymore.
Probably, you wanted to do:
self.id += id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With