I'm starting to work with Python and I've seen that there's no way to create multiple con- trollers in the same class. With this I thought of passing to the% co_conductor an object containing the attributes of the class, as in the example:
class Foo(object):
'''
classdocs
'''
_id = None
name = None
def __init__(self, *attrs):
if attrs:
for key, value in attrs.iteritems():
setattr(self, key, value)
def __getattr__(self, key):
if(hasattr(self, key)):
return key
else:
return None
def __setattr__(self, k, v):
if hasattr(self, k):
super().__setattr__(k, v)
return True
else:
return False
The problem is that I can not feed my class with the existing attributes, it always presents an error. When I create a simple instance of the class without passing attributes and manually define the attributes, the class also does not constrain if it does not exist.
from Project.Models.Foo import Foo
c = Foo()
print(c._id)
print(c.name)
print(c.foo)
When doing this I have the error:
Traceback (most recent call last):
File "F:\Project\src\Project\Test\TestFoo.py", line 7, in <module>
print(c.foo)
File "F:\Project\src\Project\Models\Foo.py", line 25, in __getattr__
if(hasattr(self, key)):
Does anyone have an idea how I can pass objects to classes and how can I restrict the get and set if the attribute does not exist?