# Abstract variables in abc

**URL:** <https://discuss.python.org/t/abstract-variables-in-abc/69071>\
**Category:** Python Help\
**Created:** [October 24, 2024, 2:43pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071 "2024-10-24T14:43:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![plswork](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/plswork/32/23570_2.png) [@plswork](https://discuss.python.org/u/plswork)\
**Post date:** [October 24, 2024, 2:43pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/1 "2024-10-24T14:43:30Z")

</div>

the ability to create abstract variables so you can encourage the user to override a class variable so you can encourage a user to override it

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [October 24, 2024, 2:58pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/2 "2024-10-24T14:58:01Z")

</div>

Use [ABC](https://docs.python.org/3/library/abc.html#abc.ABC).

```python
from abc import ABC, abstractmethod

class BaseClass(ABC):
    # Abstract method (must be overridden in subclass)
    @abstractmethod
    def do_something(self):
        pass

class SubClass(BaseClass):
    # Implementing the abstract method
    def do_something(self):
        print("Doing something!")

# Usage
sub = SubClass()
sub.do_something() # Doing something!

```

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [October 24, 2024, 3:43pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/3 "2024-10-24T15:43:51Z")

</div>

Moving to the Help category as this is not framed as a clear, detailed proposal and may already be solved by the `abc` module.

---

<div class="post-metadata">

**Author:** ![drmason13](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/drmason13/32/22591_2.png) [@drmason13](https://discuss.python.org/u/drmason13)\
**Post date:** [October 24, 2024, 6:05pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/4 "2024-10-24T18:05:31Z")

</div>

More to the point you can define abstract _properties_:

```python
from abc import ABC, abstractmethod

class BaseClass(ABC):
    # Abstract property (must be defined in subclass)
    @property
    @abstractmethod
    def required_property(self):
        pass

    @classmethod
    def provided_method(cls):
        print(cls.required_property)

class SubClass(BaseClass):
    # Implementing the abstract property
    required_property = "I am from subclass"

# Usage
SubClass.provided_method() # I am from subclass

class BadSubClass(BaseClass):
    pass

BadSubClass() # TypeError: Can't instantiate abstract class

```

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [October 25, 2024, 12:53pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/5 "2024-10-25T12:53:49Z")

</div>

> [@drmason13](#):
>
> ```python
> class SubClass(BaseClass):
> # Implementing the abstract property
> required_property = "I am from subclass"
> 
> ```

This _replaces_ the property with a `str` value. `abc` is satisfied, because it doesn’t care what gets bound to the name `required_property`, as long as _something_ does.

---

<div class="post-metadata">

**Author:** ![drmason13](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/drmason13/32/22591_2.png) [@drmason13](https://discuss.python.org/u/drmason13)\
**Post date:** [October 26, 2024, 2:05pm UTC](https://discuss.python.org/t/abstract-variables-in-abc/69071/6 "2024-10-26T14:05:07Z")

</div>

Ah, I see you are correct.

I was being lazy with syntax at the expense of semantics.

You can implement the abstract property more precisely like this:

```python
class SubClass(BaseClass):
    # Implementing the abstract property
    @property
    def required_property(cls):
        return "I am from subclass"

```

being a property you can still access it succinctly:

```python
SubClass().required_property

```

However, since they want a class variable, and class properties are (I’ve just discovered) deprecated, it might be best to override the property and make a class attribute anyway.

Unless ABC provides a way to make an abstract attribute I can’t think of a better way.
