Hello all I have a custom class MyClass. I wish to iterate over its default values. I think my issue is best shown with an example:
class MyClass:
def __iter__(self):
for each in self.__dict__.values():
yield each
first_var: str = "asdf"
second_var: str = "tasdt"
my_class = MyClass()
for var in my_class: # this does not work, how can i get this to work?
print(var)
my_class.first_var = "hello" # setting the variables makes it work
my_class.second_var = "world"
for var in my_class: # now it works
print(var)
As you can see from the example the first for loop does not print the default values of the class MyClass. How can I achieve that?
EDIT: Based on the comment by C Hecht i tried
def __iter__(self):
for attribute in self.__dict__.keys():
if attribute[:2] != '__':
value = getattr(self, attribute)
if not callable(value):
print(attribute, '=', value)
yield value
Still not getting those class attributes