-2

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)
deceze
  • 510,633
  • 85
  • 743
  • 889
user10163680
  • 253
  • 2
  • 11
  • 2
    "Why we have to mention it again"…?! In one step you're *defining* the `class Dataset`, in the next step you're *instantiating* it. It doesn't make a whole lot of sense to define a class inside a function BTW. This looks like pretty bad sample code. – deceze Aug 21 '18 at 08:33
  • 1
    See https://stackoverflow.com/questions/13886168/how-to-use-the-pass-statement-in-python – deceze Aug 21 '18 at 08:34
  • I haven't mentioned whole code. I define class Dataset consisting of several variables and functions and then I define a function read_data_set that is calling calss Dataset. I am just trying to understand this piece of code. Why it is a bad practice? – user10163680 Aug 21 '18 at 08:36
  • 2
    A class should only be defined once. By putting it inside a function, you're redefining it every time you call the function. Sometimes there can be a use for that, but usually it's just unnecessary work being done every time you call the function. – I understand why you might ask about `pass`, but I don't understand what is unclear about the rest of the code. Can you clarify what you don't understand? – deceze Aug 21 '18 at 08:40

4 Answers4

2

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
fractals
  • 816
  • 8
  • 23
2

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 
Kamil
  • 1,256
  • 10
  • 17
1

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
Sasha Tsukanov
  • 1,025
  • 9
  • 20
0

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.

Matina G
  • 1,452
  • 2
  • 14
  • 28
  • So it means that if I want to create some object inside a function( that is defined outside the class) ,First I have to again define that class with a pass statement and after that I can call constructors – user10163680 Aug 21 '18 at 08:43
  • Why class intialization is compulsory? I run the above code omitting class Dataset and pass statement it still works fine – user10163680 Aug 21 '18 at 08:51
  • @user Because you have defined the class somewhere else before? Then yeah, redefining it is pretty superfluous and counter productive. – deceze Aug 21 '18 at 08:56
  • @user10163680 If your class was declared before you call the function where you want to use it, than you don't need to redefine the class with `pass`. You might wan't to define a class inside a function only if it was not defined yet or if you wan't to change the definition. – Sasha Tsukanov Aug 21 '18 at 09:18
  • I have to creat the objects of class inside read_data_set function. It is compulsory to redefine it with pass statement – user10163680 Aug 21 '18 at 09:27
  • 1
    If your class is already defined, redefining it will annulate previous definition for the scope of your function. So the object that your function returns has none of the attributes / methods of the definition outside your function – Matina G Aug 21 '18 at 13:04