# PyCall: How can I make a python class with static attributes?

**URL:** https://discourse.julialang.org/t/pycall-how-can-i-make-a-python-class-with-static-attributes/13007
**Category:** General Usage
**Created:** [August 7, 2018, 10:35pm UTC](https://discourse.julialang.org/t/pycall-how-can-i-make-a-python-class-with-static-attributes/13007 "2018-08-07T22:35:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 7, 2018, 10:35pm UTC](https://discourse.julialang.org/t/pycall-how-can-i-make-a-python-class-with-static-attributes/13007/1 "2018-08-07T22:35:49Z")

</div>

I want to be able to capture key-press events in a Julia powered [PyQtGraph](http://pyqtgraph.org/) GUI using the amazing [PyCall](https://github.com/JuliaPy/PyCall.jl) package, but have been unable to because I can’t make python classes with static attributes using the `@pydef` macro in PyCall.

Moreover, when I try to make any class with a static attribute using `@pydef`, I get the following error:

```julia
julia> @pydef mutable struct Foo
           class_attr = 1
           function __init__ (self)
               println(self[:class_attr])
           end
       end

ERROR: AssertionError: Not a function definition: class_attr = 1

```

which is equivalent (I hope) to the following python code:

```python
class Foo(object):
    class_attr= 1
    def __init__ (self):
        print(self.class_attr)

```

Looking at [the source code](https://github.com/JuliaPy/PyCall.jl/blob/2673bfe7559ff9d7bd9056e58f08e6ed160cb737/src/pyclass.jl#L61) responsible for parsing the contents of a `@pydef` macro call makes me think that this capability is simply not yet implemented.

I have already opened [an issue](https://github.com/JuliaPy/PyCall.jl/issues/513) in PyCall, but was wondering if there is some other way to make a python class with a static attribute that I’m missing?

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [August 7, 2018, 10:44pm UTC](https://discourse.julialang.org/t/pycall-how-can-i-make-a-python-class-with-static-attributes/13007/2 "2018-08-07T22:44:09Z")

</div>

Hopefully there is a more direct answer, but if not, are you sure you need a class? qobject.connect should take a function handle. If you wanted to use a class as a closure, there’s a slightly ugly hack to do that in the function’s properties.

---

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 8, 2018, 1:03am UTC](https://discourse.julialang.org/t/pycall-how-can-i-make-a-python-class-with-static-attributes/13007/3 "2018-08-08T01:03:34Z")

</div>

Thanks I’ll try something along those lines! I think I do need a class at some point but you’re absolutely right that I don’t need to connect the class itself to the signal, nor do I really need to have the signal be a static attribute of the class.

I was going to subclass a window ([GraphicsLayoutWidget](http://pyqtgraph.org/documentation/widgets/graphicslayoutwidget.html)), add a custom signal as a static attribute, then reimplement the keyPressEvent method to emit the signal on a key press (basically [this](https://stackoverflow.com/questions/27475940/pyqt-connect-to-keypressevent)), but I could figure out some other way of doing it that woudn’t require a static attribute.
