Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python enforcing class variables in subclass

class Foo:

    page_name = None
    author = None

    def __init_subclass__(cls, **kwargs):
        for required in ('page_name', 'author',):
            if not getattr(cls, required):
                raise TypeError(f"Can't instantiate abstract class {cls.__name__} without {required} attribute defined")
        return super().__init_subclass__(**kwargs)


class Bar(Foo):
    page_name = 'New Page'
    author = 'eric'
Comment

PREVIOUS NEXT
Code Example
Python :: jupyter notebook morse code francais 
Python :: without @tf.function OOM 
Python :: is cobol obsolete 
Python :: python char to hex 
Python :: sphinx select code ' 
Python :: roll dice python 
Python :: how many orders has customer made database python 
Python :: bolumden kalan python 
Shell :: get cpu frequency linux 
Shell :: conda install tqdm 
Shell :: Starting Apache...fail. 
Shell :: woeusb ubuntu install 
Shell :: linux how to see ports in use 
Shell :: check react version 
Shell :: install git-lfs ubuntu 18.04 
Shell :: ps not found 
Shell :: bash: gedit: command not found 
Shell :: reset local branch to remote 
Shell :: firewalld not running centos 7 
Shell :: kde connect not showing devices 
Shell :: nginx stop commands 
Shell :: install ngrok ubuntu 20.04 
Shell :: bash command for unzipping tar.gz files 
Shell :: git remove file from repo that has been added to gitignore 
Shell :: install selenium python 
Shell :: docker check linux os 
Shell :: install nano ubuntu 
Shell :: copy laravel project from github 
Shell :: git show tree graph 
Shell :: install firefox homebrew 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =