I have come to python from C++ (I also know a little Java). I am writing code to exercise a server so I basically use http methods to send a load of requests to the server. As part of my code I created a class and called it testdata. This contains basically test input data (to send in requests) and output data to check against.
With my C++ hat on I created a load of getter and setter methods which were one lines. I even had an
__init__ method to set eg
self.<some_attribute_name> = "" or some safe default.
I realise now that you can freely add attributes to a python object and there is no need to pre-initialise simple variables in init.
So I have some questions.
For my testdata class, should I even be using a class?
I just add data items like this:
import testing td = testing.testdata() td.sessionid = "123"
But what about access eg to
td.sessionidbefore I have previously assigned a value. I know I can use
hasattrbut that is an extra burden. Do I have to make sure I always assign first? What if I do
td.sessionidbefore it is even assigned anything? I will get an AttributeError at runtime. How to handle this?
Due to problem 3. above. Should I always use
__init__to initialise member data to safe default values? eg "" for strings.
What are recommended approaches in Python?