I've been stuck on this for a while. I cannot access the value of an object's attribute once constructed, but console prints the names just fine. The names and number of attributes needs to be dynamic and change depending on what source they come from. Therefore I can't just use a consistent way.
starting_list = ["Comapny", "Contact Name", "Email", "Phone", "URL", "Country", "Source"]
class NewClass(object):
_myattrib = None
def __setattr__(self, prop, val):
super().__setattr__('myattrib', val)
print("setting myattrib")
def __init__(self):
for item in starting_list:
#cleaning up a bit
item = str(item)
item = item.lower()
item = item.strip()
item = item.replace(' ', '_')
self.__setattr__(self, item, 'hi')
print(item) #it reads it here
print(NewClass.url)
any help would be apprecieated.
__setattr__()method to always set the attributemyattribrather than using the actual attribute name, so I presume it's doing what you told it to.__setattr__()isn't called directly. Also, the way you're calling it looks like it would raise an error. Trysetattr(self, name, value). Also,str(item)doesn't do anything useful.