Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从同一文件到不同文件再到_uinit__; py.py实例化python类_Python_Python 3.x - Fatal编程技术网

从同一文件到不同文件再到_uinit__; py.py实例化python类

从同一文件到不同文件再到_uinit__; py.py实例化python类,python,python-3.x,Python,Python 3.x,我是python的新手, Q1:我想知道,为什么print语句执行多次,即使我只调用了school1.print\u name()一次 问题2:在创建新代码时创建的\uuuu init\uuuu.py的目的是什么 包裹 问题3:从otherfile.py创建对象和 \uuuu init\uuuuu.py (注意:我调试了otherfile.py和init.py中的代码,其中脚本在调用school1.print_name()后不会终止,但它会从脚本开始重新运行) SameFile.py class

我是python的新手, Q1:我想知道,为什么print语句执行多次,即使我只调用了
school1.print\u name()
一次

问题2:在创建新代码时创建的
\uuuu init\uuuu.py
的目的是什么 包裹

问题3:从
otherfile.py
创建对象和
\uuuu init\uuuuu.py

(注意:我调试了otherfile.py和init.py中的代码,其中脚本在调用school1.print_name()后不会终止,但它会从脚本开始重新运行)

SameFile.py

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)

school1 = School("DPS") # 1 
school1.print_name()`   # 2
# These 1 and 2 lines are removed from this file when running from otherfile.py and __init__.py
import School

school1 = School("DPS")
school1.print_name()
import School

school1 = School("DPS")
school1.print_name()
o/p:

School name is : DPS
School name is : DPS
School name is : DPS #why did it print second time
School name is : DPS
School name is : DPS #why did it print second time
OtherFile.py

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)

school1 = School("DPS") # 1 
school1.print_name()`   # 2
# These 1 and 2 lines are removed from this file when running from otherfile.py and __init__.py
import School

school1 = School("DPS")
school1.print_name()
import School

school1 = School("DPS")
school1.print_name()
o/p:

School name is : DPS
School name is : DPS
School name is : DPS #why did it print second time
School name is : DPS
School name is : DPS #why did it print second time
\uuuu init\uuuuu.py

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)

school1 = School("DPS") # 1 
school1.print_name()`   # 2
# These 1 and 2 lines are removed from this file when running from otherfile.py and __init__.py
import School

school1 = School("DPS")
school1.print_name()
import School

school1 = School("DPS")
school1.print_name()
o/p:

School name is : DPS
School name is : DPS
School name is : DPS #why did it print second time
School name is : DPS
School name is : DPS #why did it print second time

导入模块时,将执行该模块中的函数。要防止此问题,可以使用`ifname==“main”:

关于第二季度和第三季度,请参考以下问题:
导入模块时,将执行该模块中的功能。要防止此问题,可以使用`ifname==“main”:

关于第二季度和第三季度,请参考以下问题:
考虑以下文件结构:

mymodule/
    __init__.py
    Buildings.py
让我们暂时保持
\uuuu init\uuuu.py
为空<代码>建筑物.py如下所示:

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)


if __name__ == '__main__':
    school1 = School("DPS -- Buildings.py")
    school1.print_name()
from mymodule.Buildings import School

school = School('DPS - test.py')
school.print_name()
在文件
test.py
中使用模块,如下所示:

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)


if __name__ == '__main__':
    school1 = School("DPS -- Buildings.py")
    school1.print_name()
from mymodule.Buildings import School

school = School('DPS - test.py')
school.print_name()
正如我们所期望的那样,这将正确地打印DPS-test.py。由于
\uuuu init\uuuu.py
为空,因此不会打印其他内容。但是,如果我们在
\uuuu init\uuuu.py
中放入任何打印语句,则从
mymodule
模块导入任何内容时都会显示该语句

更改
\uuuu init\uuuu.py

from mymodule.Buildings import School 

print('__init__.py is loaded!')

school = School('DPS - __init__.py')
school.print_name()
然后运行
test.py
,这将产生以下输出:

__init__.py is loaded!
School name is : DPS - __init__.py
School name is : DPS - test.py

因此,您可以看到,在加载模块时首先执行
\uuuu init\uuuuuuuuuupy
,然后执行
test.py
中的其他行。

考虑以下文件结构:

mymodule/
    __init__.py
    Buildings.py
让我们暂时保持
\uuuu init\uuuu.py
为空<代码>建筑物.py如下所示:

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)


if __name__ == '__main__':
    school1 = School("DPS -- Buildings.py")
    school1.print_name()
from mymodule.Buildings import School

school = School('DPS - test.py')
school.print_name()
在文件
test.py
中使用模块,如下所示:

class School:

    def __init__(self, name):
        self.name = name

    def print_name(self):
        print("School name is : " + self.name)


if __name__ == '__main__':
    school1 = School("DPS -- Buildings.py")
    school1.print_name()
from mymodule.Buildings import School

school = School('DPS - test.py')
school.print_name()
正如我们所期望的那样,这将正确地打印DPS-test.py。由于
\uuuu init\uuuu.py
为空,因此不会打印其他内容。但是,如果我们在
\uuuu init\uuuu.py
中放入任何打印语句,则从
mymodule
模块导入任何内容时都会显示该语句

更改
\uuuu init\uuuu.py

from mymodule.Buildings import School 

print('__init__.py is loaded!')

school = School('DPS - __init__.py')
school.print_name()
然后运行
test.py
,这将产生以下输出:

__init__.py is loaded!
School name is : DPS - __init__.py
School name is : DPS - test.py

因此,您可以看到,在加载模块时,首先执行
\uuu init\uuuuuuuuuuuuuuuuupy
,然后执行
test.py
中的其他行。

如果uuuuuuuu name=“uuuuuuuuuuuuuuuu main”,您应该将
添加到定义类
学校的文件中,请参阅文章。在文件中给你的学校起不同的名字,看看实际发生了什么!谢谢@rinkert!!弄明白了,到底发生了什么。感谢:)在
\uuuuu init\uuuuuuuu.py
假设
school1=School(“DPS--u init_________)
school1.print_name()
--O/P:
学校名称是:DPS--u init____
它现在打印多次了吗?现在发生了什么?你最好将你的
\uuuu init\uuuuuuuuuuy.py保持为空,将name==main添加到定义
学校的文件中,并在需要
学校的文件顶部执行
导入学校
。如果
学校的文件中定义了
学校,你应该添加
,见下文。在文件中给你的学校起不同的名字,看看实际发生了什么!谢谢@rinkert!!弄明白了,到底发生了什么。感谢:)在
\uuuuu init\uuuuuuuu.py
假设
school1=School(“DPS--u init_________)
school1.print_name()
--O/P:
学校名称是:DPS--u init____
它现在打印多次了吗?现在发生了什么?您最好将
\uuuu init\uuuuuuuuy.py
留空,将name==main添加到定义
学校的文件中,并在需要
学校的文件顶部执行
导入学校
。如果
除非这些函数是在模块中执行的
\uuuu init\uuuuuupy
@MatthewBarlowe,我能知道你在哪里链接上述点吗?thanks@RahulReddy注释说“当您导入一个模块时,该模块中的函数被执行”,这是不正确的,除非这些函数被指定在modules
\uuuu init\uu
.py文件中运行__“:
与导入文件时发生的情况无关,而是当文件作为独立脚本在命令行上运行时,模块中的函数在导入时不执行,无论是否使用
,如果uuuuu name\uuuuuu==“uuu main\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,我能知道你在哪里把上述观点联系起来吗?thanks@RahulReddy注释说“当您导入一个模块时,该模块中的函数被执行”,这是不正确的,除非这些函数被指定在modules
\uuuu init\uu
.py文件中运行__“:
与导入文件时发生的情况无关,而是与文件作为独立脚本在命令行上运行时发生的情况无关。是的,这很好@rinkert!!假设我正在运行来自
\uuuu init\uuuuuuuuuupy
的代码,那么输出结果如下:-----------------
\uuuuuuuinit\uuuuuuupy已加载<代码>学校名称为:DPS-uuu init_uuuuuuuuuupy
<已加载代码>\uuuu init\uuuuuuuuuuuuuuy.py