# Object as vertex attribute

**URL:** <https://igraph.discourse.group/t/object-as-vertex-attribute/197>\
**Category:** Usage\
**Tags:** Python\
**Created:** [29 April 2020 18:17 UTC](https://igraph.discourse.group/t/object-as-vertex-attribute/197 "2020-04-29T18:17:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sothl](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@sothl](https://igraph.discourse.group/u/sothl)\
**Post date:** [29 April 2020 18:17 UTC](https://igraph.discourse.group/t/object-as-vertex-attribute/197/1 "2020-04-29T18:17:49Z")

</div>

I am trying to create a graph with iGraph whose vertices have some attributes, like an object from a class that I defined. Its constructor is as follows:

```auto
class Tauler:

def __init__ (self, num_files, num_columnes):
    self.files, self.columnes = num_files, num_columnes
    self.tauler = {i: [0] * num_columnes for i in range(num_files)}

```

When I try to assign a `Tauler` object (t1) as an attribute of some vertex, I get the following error:

```auto
g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})
Traceback (most recent call last):

  File "<ipython-input-10-9167be7715d9>", line 1, in <module>
    g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})

  File "C:\Users\Stohl\anaconda3\lib\site-packages\igraph\ __init__.py", line 232, in __init__
    self.vs[key] = value

TypeError: object of type 'Tauler' has no len()

```

If I then try to define the **len** method like so:

```auto
def __len__ (self):
    return self.tauler

```

I get the next error:

```auto
g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})
Traceback (most recent call last):

  File "<ipython-input-13-9167be7715d9>", line 1, in <module>
    g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})

  File "C:\Users\Stohl\anaconda3\lib\site-packages\igraph\ __init__.py", line 232, in __init__
    self.vs[key] = value

TypeError: 'dict' object cannot be interpreted as an integer

```

If I try to use the pickle module, then the following happens:

```auto
g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": pickle.dumps(t1)})
g.vs[0]
igraph.Vertex(<igraph.Graph object at 0x000000000960CB88>, 0, {'tauler': 128})
# Why 128?? Why it is even an int?
pickle.loads(g.vs[0]["tauler"])
Traceback (most recent call last):

  File "<ipython-input-30-3b1c9e9c841a>", line 1, in <module>
    pickle.loads(g.vs[0]["tauler"])

TypeError: a bytes-like object is required, not 'int'

```

However, if I use the `pickle.dumps()` at the IPython shell, it returns a bytes-object and not an int. What is the problem here? What am I missing? Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [29 April 2020 18:20 UTC](https://igraph.discourse.group/t/object-as-vertex-attribute/197/2 "2020-04-29T18:20:21Z")

</div>

I think you should supply a list. Try

```auto
g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": [t1]})

```

---

<div class="post-metadata">

**Author:** ![sothl](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@sothl](https://igraph.discourse.group/u/sothl)\
**Post date:** [29 April 2020 18:38 UTC](https://igraph.discourse.group/t/object-as-vertex-attribute/197/3 "2020-04-29T18:38:36Z")

</div>

Yes, it worked! Many thanks 🙂
