I am a newbie in object oriented programming. After defining a class we simply create objects and try to access different variables and functions inside the class. In the following code I want to know that why we again have to mention class Dataset inside the function ans secondly what is the role of pass statement?
def read_data_set():
class Dataset:
pass
data_sets = Dataset()
return(data_sets)
#Function call
x=read_data_set()
print(x)
It basically does nothing.
It is often used as a placeholder like in your code; you'll notice that the code does not run at all without the pass
there.
class SomeClass:
pass # to be filled
This is because Python expects something to be under the definition of SomeClass
, but being empty it raises an IndentationError
.
class SomeClass:
# to be filled <--- empty
other_function() # <--- IndentationError
It is also used with try
- except
when you do not want to do anything:
try:
some_stuff()
except SomeException:
pass # I don't mind this
pass
does nothing, it just makes Python indentations correct.
Let's say you want to create an empty function, sth like:
def empty_func():
empty_func() # throws IndentationError
Let's try to put a comment inside:
def empty_func_with_comment():
# empty
empty_func_with_comment() # throws IndentationError
To make it work we need to fix indentations by using pass:
def empty_func_with_pass():
pass
empty_func_with_pass() # it works
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
The pass statement means that you initialise your class without defining a constructor or any attributes. Try ommitting it : The error that you see is due to the fact that python will expect the following line to belong to your class - and will consider that the indentation used is not correct.
Regarding the fact that your class name is called again inside your function : it means that you are instanciating the class you just defined. Thus, what your function returns is an object of your class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.