Why do we mention class Dataset inside the function twice?
The first time
class Dataset:
pass
the class is defined,
and the second one:
data_sets = Dataset()
an instance of this class (an object) is created. Exactly as the OP has written:
After defining a class we simply create objects.
Since class
is just a python statement it can be used anywhere: including function bodies, like in this case. So here the class is defined each time the function read_data_set()
is called and is not defined at all if it is not called.
What is the role of pass statement?
In this example
class Dataset:
pass
the pass
statement means defining a class with no members added inside it. It means that the class and its objects contain only some "default" functions and variables (aka methods and fields) that are derived from object
by any class.
In general pass
is used when you introduce a new block and want to leave it empty:
expression:
pass
You must include at least one instruction inside a block, that's why sometimes you need pass
to say that you don't want to do anything inside the block. It is true for functions:
def do_nothing():
pass # function that does nothing
loops:
for i in collection: # just walk through the collection
pass # but do nothing at each iteration
exception handling:
try:
do_something()
except SomeException:
pass # silently ignore SomeException
context managers:
with open(filename): # open a file but do nothing with it
pass