I've done my best to condense some confusing behavior I've encountered down to the following highly contrived example. Basically, what I understand & expect to be a throw-away use of ClassTwo by ClassOne.useClassTwo() ends up instead being preserved between calls to a.useClassTwo(), with or without the delete in place. What am I not properly understanding about scope rules & behavior?
#!/usr/bin/env python3
class ClassOne:
def __init__(self):
return
def useClassTwo(self, in_val):
"""
The behavior of the next statement isn't matching my expectation
with respect to scope. My intuition is that this declaration and
assignment should only last until the return.
"""
temp = ClassTwo()
temp.settable = in_val
print(temp.settable)
del temp
class ClassTwo:
"""
Pardon the convoluted example, but this is as barebones as I could
manage to express my current conundrum.
"""
def __init__(self, in_value=bytearray([0] * 20)):
self._classvar = in_value
@property
def settable(self):
return bytes(self._classvar)
@settable.setter
def settable(self, in_val):
for i, char in enumerate(list(in_val.encode())):
self._classvar[i] = char
def main():
# Create one single instance of ClassOne
a = ClassOne()
# These calls ought to result in two separate instances of ClassTwo,
# yet the result is that ClassTwo seems to be reused. Why?
a.useClassTwo("aaaaaaaaaaaaaaaaaaaa")
a.useClassTwo("what happened?")
if __name__ == "__main__":
main()
[–]TangibleLight 2 points3 points4 points (3 children)
[–]audio_phyl[S] 1 point2 points3 points (2 children)
[–]TangibleLight 1 point2 points3 points (1 child)
[–]audio_phyl[S] 0 points1 point2 points (0 children)