Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
added 4 characters in body
Source Link
gebbissimo

If you don't have a 3.7 for @dataclass and need mutability, the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 : int = None
        self.var2 : str = None 

    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instance variable {} is still None".format(key)
        return True 


params = Params()
params.var1 = 2
params.var2 = 'hello'
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability, the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 : int = None
        self.var2 : str = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instance variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello'
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability, the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 : int = None
        self.var2 : str = None 

    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instance variable {} is still None".format(key)
        return True 


params = Params()
params.var1 = 2
params.var2 = 'hello'
assert(params.are_all_defined)
added type hinting
Source Link
gebbissimo

If you don't have a 3.7 for @dataclass and need mutability, the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 : int = None
        self.var2 : str = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instances"instance variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello'hello'
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 = None
        self.var2 = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instances variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability, the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 : int = None
        self.var2 : str = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instance variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello'
assert(params.are_all_defined)
added 37 characters in body
Source Link
gebbissimo

If you don't have a 3.7 for @dataclass and need mutability thisthe following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is easyvery simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 = None
        self.var2 = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instances variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability this might work for you. It's quite self-documenting and IDE-friendly, prevents writing things twice, is easily extendable and it is easy to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 = None
        self.var2 = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instances variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello
assert(params.are_all_defined)

If you don't have a 3.7 for @dataclass and need mutability the following code might work for you. It's quite self-documenting and IDE-friendly (auto-complete), prevents writing things twice, is easily extendable and it is very simple to test that all instance variables are completely initialized:

class Params():
    def __init__(self):
        self.var1 = None
        self.var2 = None
    def are_all_defined(self):
        for key, value in self.__dict__.items():
            assert (value is not None), "instances variable {} is still None".format(key)
        return True

params = Params()
params.var1 = 2
params.var2 = 'hello
assert(params.are_all_defined)
Source Link
gebbissimo
Loading
lang-py