Reputation:
i Have a class Vertex
like:
class Vertex:
def __init__(self,key):
self.id = key
self.connectedTo = {}
self.neighbors = {}
self.dict = {}
self.community = []
self.inside = 0
self.been = 0
and I want to put all those selfs community, inside, been, (...)
and some more attributes in one dictionary that will be iterable so:
vertex['inside'] = 0
Right now some of my attributes are in self.dict
so I'm using for example: vertex.dict['visited'] = 1
but can I skip .dict
and write it as vertex['visited'] = 1
?
Upvotes: 0
Views: 54
Reputation: 123413
You can create a class like that by overriding the special methods associated with accessing the elements as illustrated below:
class Vertex:
def __init__(self, key):
super().__setattr__('_dict', {})
self['id'] = key
self['connectedTo'] = {}
self['neighbors'] = {}
self['community'] = []
self['inside'] = 0
self['been'] = 0
def __setitem__(self, key, value):
self._dict[key] = value
def __setattr__(self, name, value):
_dict = super().__getattribute__('_dict')
_dict[name] = value
def __getattr__(self, name):
_dict = super().__getattribute__('_dict')
return _dict[name]
vertex = Vertex(42)
print('Upon creation:')
print(f'{vertex._dict=}')
vertex.community.append((1,2,3))
vertex.inside = 17
vertex.connectedTo['foobar'] = 20210613
print()
print('After modifications:')
print(f'{vertex._dict=}')
Output:
Upon creation:
vertex._dict={'id': 42, 'connectedTo': {}, 'neighbors': {}, 'community': [], 'inside': 0, 'been': 0}
After modifications:
vertex._dict={'id': 42, 'connectedTo': {'foobar': 20210613}, 'neighbors': {}, 'community': [(1, 2, 3)], 'inside': 17, 'been': 0}
Upvotes: 0
Reputation: 338
You need to use the getitem and setitem methode like :
class Vertex:
def __init__(self,key):
...
self.data = {'community':self.community, ...}
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
Need to find something else for the setitem method this is not going to work perfectly here, it was just an example, but the getitem will work for sure.
Why it will not work :
class Vertex:
def __init__(self,key):
self.a = 4
self.data = {'a': self.a}
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
then :
>>> t = Vertex(1)
>>> t['a']
4
>>> t['a'] = 3
>>> t['a']
3
>>> t.a
4
The value in the dict will be updated but not the attribute
Upvotes: 0
Reputation: 50809
You can use __dict__
on an instance of Vertex
vertex = Vertex(1)
d = vertex.__dict__
d['visited'] = 1
print(vertex.visited) # 1
Upvotes: 1