# Using exec to define a member function that uses super()

**URL:** <https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 28, 2026, 11:26am UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731 "2026-10-28T11:26:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![crj](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@crj](https://discuss.python.org/u/crj)\
**Post date:** [October 28, 2026, 11:26am UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/1 "2026-10-28T11:26:52Z")

</div>

Suppose I am defining a class. This works:

```python
class Foo:
    exec('def f(self): pass')

```

However, things go wrong if the function defined that way uses `super()`:

```python
class Base:
    pass
class Derived(Base):
    exec('def f(self): super()')

```

```python-repl
>>> Derived().f()
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    Derived().f()
    ~~~~~~~~~~~ ^^
  File "<string>", line 1, in f
RuntimeError: super(): __class__ cell not found

```

It’s not wrong: `Derived.f. __code__.co_freevars` is `()` and `Derived.f. __closure__ ` is `None`. The effect is equivalent to:

```python
class Base:
    pass
def f(self): super()
class Derived(Base):
    f = f

```

Is there any way to tease `exec()` (or something else) into doing what I hoped for?

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [October 28, 2026, 1:01pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/2 "2026-10-28T13:01:45Z")

</div>

Why would you want to do this?

---

<div class="post-metadata">

**Author:** ![crj](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@crj](https://discuss.python.org/u/crj)\
**Post date:** [October 28, 2026, 2:01pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/3 "2026-10-28T14:01:43Z")

</div>

To mechanically generate complex member functions.

Yes, you could `eval()` the entire class, or generate an entire `.py` file, but this is a case where most of the code is straightforward and I only want to generate one or two functions.

An ugly workaround is to `Base.method(self, ...)` instead of `super().method(...)`, but I confess at this point I’m at least as interested in filling a hole in my knowledge concerning how classes are built and whether that functionality is user-accessible.

Having prodded things a little more, I infer that:

- The special behaviour is implemented in compiling the AST to bytecode
- It seems to be triggered by the presence of a `Name('super')` within the syntax of a function definition within a class.

Honestly, that feels a tiny bit hacky…

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [October 28, 2026, 2:16pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/4 "2026-10-28T14:16:35Z")

</div>

I don’t think `exec` was designed for this type of thing, and I would instead look into metaclasses. But could you show examples of the real sorts of methods you’re trying to auto-generate?

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [October 28, 2026, 2:20pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/5 "2026-10-28T14:20:36Z")

</div>

> [@crj](#):
>
> To mechanically generate complex member functions.

While I’m not familiar with the actual internals involved, I do know that `dataclasses` does this. Perhaps that might be a good reference for how best to implement such a thing?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 28, 2026, 2:26pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/6 "2026-10-28T14:26:36Z")

</div>

> [@crj](#):
>
> An ugly workaround is to `Base.method(self, ...)` instead of `super().method(...)`, but I confess at this point I’m at least as interested in filling a hole in my knowledge concerning how classes are built and whether that functionality is user-accessible.

Are you able to use the two-arg version instead? `super(Derived, self).whatever()` ? The zero-arg version is a very nice convenience, but it’s ultimately syntactic sugar for that. (The class name is whatever one you’re currently defining, no need to pick a base class or anything.)

---

<div class="post-metadata">

**Author:** ![crj](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@crj](https://discuss.python.org/u/crj)\
**Post date:** [October 28, 2026, 2:37pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/7 "2026-10-28T14:37:04Z")

</div>

…good point. Feeling stupid for not thinking of that. 😳

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 28, 2026, 2:39pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/8 "2026-10-28T14:39:15Z")

</div>

> [@crj](#):
>
> Feeling stupid for not thinking of that.

It’s (almost) always something simple - the work of debugging is working out which if the tens of thousands of simple things is the one that’s the problem here. No need to feel stupid for not thinking of the exact one that it turned out to be!

---

<div class="post-metadata">

**Author:** ![DavidCEllis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidcellis/32/9789_2.png) [@DavidCEllis](https://discuss.python.org/u/DavidCEllis)\
**Post date:** [October 28, 2026, 4:09pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/9 "2026-10-28T16:09:25Z")

</div>

There are a few ways you _can_ make an `exec`-ed function work with a no arg super, although it’s a little sneaky.

Essentially you want to get your function from inside a function that defines ` __class__ ` inside the string you’re exec-ing with a way to reassign ` __class__ `. Providing you don’t need to actually perform your `exec` call or assignment inside the class body you can do something like this.

```python
s = """
def maker( __class__ ):
    def f(self):
        super().f()
        print(f"and {type(self). __name__ }")
    return f
"""
globs = {}
exec(s, globs)
maker = globs["maker"]

class Base:
    def f(self):
        print(f"From Base")

class Derived(Base):
    pass

Derived.f = maker(Derived)
Derived().f()

```

```md
From Base
and Derived

```

It’s also possible to patch ` __class__ ` inside the closure cell from a method defined on another class (you can see this kind of work in `dataclasses` where methods on a dataclass are patched to work on a new slotted copy of the class).

---

<div class="post-metadata">

**Author:** ![crj](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@crj](https://discuss.python.org/u/crj)\
**Post date:** [October 28, 2026, 8:41pm UTC](https://discuss.python.org/t/using-exec-to-define-a-member-function-that-uses-super/109731/10 "2026-10-28T20:41:11Z")

</div>

Ooh, sneaky. I like it!
