与进口python有点混淆 [英] Little confused with import python

查看:150
本文介绍了与进口python有点混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自PHP(以及其他一些东西)背景,我正在玩Python。在PHP中,当我想要包含另一个文件时,我只需要 include require ,并且包含该文件中的所有内容。

I come from a PHP (as well as a bunch of other stuff) background and I am playing around with Python. In PHP when I want to include another file I just do include or require and everything in that file is included.

但似乎在python中做推荐的方法似乎是来自文件导入的 但是这似乎更适合包括图书馆和东西?你如何在几个文件中分离你的代码?是唯一的方法,让一个文件包含一大堆函数调用,然后导入15个其他文件?

But it seems the recommended way to do stuff in python is from file import but that seems to be more for including libraries and stuff? How do you separate your code amongst several files? Is the only way to do it, to have a single file with a whole bunch of function calls and then import 15 other files?

推荐答案

PHP和Python之间的情况完全不同,原因很多。

Things are totally different between PHP and Python, and there are many reasons why.


但似乎推荐的做法是python是从文件导入但是这似乎更适合包含库和东西?

But it seems the recommended way to do stuff in python is from file import but that seems to be more for including libraries and stuff?

实际上, import 语句用于将对象从另一个模块导入当前模块。您可以将导入模块的所有对象导入当前模块:

Indeed, import statements are for importing objects from another module to current module. You can either import all the objects of the imported module to current module:

import foo

print foo.bar

或者您可以从该模块中选择您想要的内容:

or you can select what you want from that module:

from foo import bar

print bar

甚至更好,如果您导入模块两次,它只会导入一次:

and even better, if you import a module twice, it will be only imported once:

>> import foo as foo1
>> import foo as foo2
>> foo1 is foo2
True




你如何分开你的代码在几个文件中?

How do you separate your code amongst several files?

你必须考虑你的代码......这就是所谓的软件设计,这里有一些规则:

You have to think about your code... That's called software design, and here are a few rules:


  • 你从不在模块的级别编写算法;而是使它成为一个函数,并调用该函数

  • 你永远不会在模块的级别实例化一个对象;你应该把它嵌入到函数中,并调用那个函数

  • 如果你需要一个不同函数的对象,创建一个类并将该对象封装在该类中,然后在你的函数中使用它绑定到该类(因此它们现在称为方法)

唯一的例外是当您想从命令行启动程序时,你追加:

The only exception is when you want to launch a program from command line, you append:

if __name__ == "__main__":

在模块的末尾。我最好的建议是在之后调用你的第一个函数:

at the end of the module. And my best advice would be to just call your first function afterwards:

if __name__ == "__main__":
    main()




这是唯一的方法,有一个单个文件有一大堆函数调用,然后导入15个其他文件?

Is the only way to do it, to have a single file with a whole bunch of function calls and then import 15 other files?

这不是唯一的方法,但它是最好的方法。您将所有算法都放入函数和对象库中,然后在其他库等中导入完全所需的内容。这就是您如何创建可重用代码的整个世界,而不必重新发明轮子!所以忘记文件,考虑包含对象的模块。

It's not the only way to do it, but it's the best way to do it. You make all your algorithms into libraries of functions and objects, and then import exactly what you need in other libraries etc.. That's how you create a whole universe of reusable code and never have to reinvent the wheel! So forget about files, and think about modules that contains objects.

最后,我对你学习python的最好建议是忘掉的每一个习惯和编写PHP时的用法,并以不同的方式再次学习这些东西。最后,这只能让你成为一名更好的软件工程师。

Finally, my best advice to you learning python is to unlearn every habit and usage you had while coding PHP, and learn those things again, differently. In the end, that can only make you a better software engineer.

这篇关于与进口python有点混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆