简体   繁体   English

pass 语句在函数中的作用?

[英]Role of pass statement inside a function?

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?在下面的代码中我想知道为什么我们在函数内部又要提到类Dataset,其次pass语句的作用是什么?

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.您会注意到,如果没有pass ,代码根本不会运行。

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 .这是因为 Python 期望某些东西在SomeClass的定义之下,但为空会引发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 - except您不想做任何事情:

try:
    some_stuff()
except SomeException:
    pass # I don't mind this

pass does nothing, it just makes Python indentations correct. pass什么都不做,它只是使 Python 缩进正确。


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:为了使它工作,我们需要使用 pass 来修复缩进:

def empty_func_with_pass():
    pass

empty_func_with_pass()  # it works 

Why do we mention class Dataset inside the function twice?为什么我们在函数内部两次提到类 Dataset ?

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:正如OP所写的那样:

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.由于class只是一个 python 语句,它可以在任何地方使用:包括函数体,就像在这种情况下一样。 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.所以这里类是在每次调用函数read_data_set()定义的,如果不调用则根本不定义。


What is the role of pass statement? pass语句的作用是什么?

In this example在这个例子中

class Dataset:
    pass

the pass statement means defining a class with no members added inside it. pass语句意味着定义一个类,其中没有添加任何成员。 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.这意味着该类及其对象仅包含一些“默认”函数和变量(也称为方法和字段),它们由任何类从object派生而来。


In general pass is used when you introduce a new block and want to leave it empty:通常,当您引入一个新块并希望将其留空时,会使用pass

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.你必须在一个块中至少包含一条指令,这就是为什么有时你需要pass来表示你不想在块内做任何事情。 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. pass 语句意味着您在不定义构造函数或任何属性的情况下初始化您的类。 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.尝试省略它:您看到的错误是由于 python 会期望以下行属于您的类 - 并且会认为使用的缩进不正确。

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.因此,您的函数返回的是您的类的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM