how to declare uninitialized variable in class definition in python
Asked Answered
Z

3

10

I come from a MATLAB background. When I create class definitions, I can instantiate "empty" variable names and then later assign values or objects to them. I.e.


classdef myclass < handle

    properties
        var1
        var2
    end 
end

a = myClass;
a.var1 = someOtherClassObject;

How do I do this in Python? I tried something like this:

class myClass:
def __init__(self):
    var1 
    var2

a = myClass()
a.var1 = someOtherClassObject()

But that's not correct. The main purpose is to build a definition of my class, like a structure, and then later go and instantiate the variables as needed.

And help would be appreciated.

Zouave answered 17/7, 2015 at 1:35 Comment(1)
Thank you very much. That's what I needed "None". (Btw, good eye, I forgot the "self" in my example)Zouave
P
19

You need to use self. to create variables for instances (objects)

I do not think you can have an uninitialized name in python, instead why not just initialize your instance variables to None ? Example -

class myClass:
    def __init__(self):
        self.var1 = None 
        self.var2 = None

You can later go and set them to whatever you want using -

a = myClass()
a.var1 = someOtherClassObject

If you need to define class variables (that are shared across instances) , you need to define them outside the __init__() method, directly inside the class as -

class myClass:
    var1 = None
    var2 = None
    def __init__(self):
        pass
Prober answered 17/7, 2015 at 1:40 Comment(2)
Thanks. I don't like adding properties/fields/attributes dynamically. I like a definition of "allowable" fields. But thank you for the value able info.Zouave
Also, please note, that like said in the answer by @BlackCat , it would be advised to create all instance variables you are going to use inside the __init__() function , especially if you are going to use those variables later in other class methods, some compilers even complaint when you define instance variables outside __init__() function.Prober
A
2

The accepted answer does not consider type-checking.

The answer would not work properly if the variable has a type, because None is a different type.

You could leverage python class variable and instance variable logic(When you create a class variable and updates it self.variable it becomes an instance variable).

With this method when you introduce types linters would not complain of None types.

Anglesey answered 20/1, 2023 at 14:1 Comment(0)
S
1

There is no such thing as "unitialized variables" in Python as you have in MATLAB, because they are not necessary. If you want to add variables to a class instance later, just add them:

>>> class myclass:
>>>     pass
>>> 
>>> a = myclass()
>>> a.var1 = 5
>>> a.var1
5

You can also add them directly to the class, so they appear in all instances of the class. This also adds them to instances of the class created earlier, so it can be dangerous if you aren't careful:

>>> class myclass:
>>>     pass
>>> 
>>> a = myclass()
>>> a.var1
AttributeError: 'myclass' object has no attribute 'var1'
>>>
>>> myclass.var1 = 5
>>> a.var1
5

You don't want to do this in MATLAB because you have to use addprop to add it first, which is needlessly complicated. Python is a more dynamic language than MATLAB in many cases. Rather than initializing something, then doing it, in Python you just do it.

Technically they are not "variables", in Python they are called "attributes" and in MATLAB they are called "properties" (Python has something called properties, but they are not the same as properties in MATLAB).

Sportsman answered 17/7, 2015 at 7:19 Comment(2)
It may not be a good idea to rely on user to create variables for instances outside the class, especially if the variables are going to be used inside instance methods. That can lead to wrong behaviours, if the user does not correctly create those variables, before calling the methodProber
That depends on what the user wants to do with the attributes. If they are going to be used in methods, then definitely it is not a good idea, just as it wouldn't be a good idea to do so in MATLAB with undefined properties. But if you just want to store some custom data for your own use, there is absolutely nothing wrong with it.Sportsman

© 2022 - 2024 — McMap. All rights reserved.