how to declare uninitialized variable in class definition in python -
i come matlab background. when create class definitions, can instantiate "empty" variable names , later assign values or objects them. i.e.
classdef myclass < handle properties var1 var2 end end = myclass; a.var1 = someotherclassobject;
how do in python? tried this:
class myclass: def __init__(self): var1 var2 = myclass() a.var1 = someotherclassobject()
but that's not correct. main purpose build definition of class, structure, , later go , instantiate variables needed.
and appreciated.
you need use self.
create variables instances (objects)
i not think can have uninitialized name in python, instead why not initialize instance variables none
? example -
class myclass: def __init__(self): self.var1 = none self.var2 = none
you can later go , set them whatever want using -
a = myclass() a.var1 = someotherclassobject
if need define class variables (that shared across instances) , need define them outside __init__()
method, directly inside class -
class myclass: var1 = none var2 = none def __init__(self): pass
Comments
Post a Comment