I am trying to create a class with various data which are read in from several files. The usual way is probably to define a constructor (__init__
) and read data inside this routine, e.g.,
from SomeOtherMod import reader # some data reader
class Myclass:
def __init__( self ):
self.N = reader.readN()
self.data = reader.readdata()
self.foo = self.data.foo()
self.bar = self.data.bar()
... # more data to read and associated properties follow
def othefunc( self ):
... # use self.N, self.data, ...
But it also seems that I can write the same thing in a header part of the class without using __init__
, e.g.,
class Myclass:
N = reader.readN()
data = reader.readdata()
foo = data.foo()
bar = data.bar()
...
def otherfunc( self ):
...
which looks more terse than the first code. So I am wondering whether this second code is a valid way for defining various fields of a Python class? Is it considered bad practice to do so, or is there any difference between the first and second approaches? I would appreciate any suggestions because I'm still new to Python. Thanks much!