简体   繁体   English

与进口python有点混淆

[英]Little confused with import python

I come from a PHP (as well as a bunch of other stuff) background and I am playing around with Python. 我来自PHP(以及其他一些东西)背景,我正在玩Python。 In PHP when I want to include another file I just do include or require and everything in that file is included. 在PHP中,当我想要包含另一个文件时,我只是includerequire并且包含该文件中的所有内容。

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? 但似乎在python中做推荐的方法似乎是from file import但这似乎更多的包括库和东西? 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? 是唯一的方法,使用一大堆函数调用单个文件,然后导入15个其他文件?

Things are totally different between PHP and Python, and there are many reasons why. PHP和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? 但似乎在python中做推荐的方法似乎是from file import但这似乎更多的包括库和东西?

Indeed, import statements are for importing objects from another module to current module. 实际上, import语句用于将对象从另一个模块导入当前模块。 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: 你必须考虑你的代码......这就是所谓的软件设计,这里有一些规则:

  • you never write an algorithm at the module's level; 你永远不会在模块的水平上写一个算法; instead make it a function, and call that function 而是使它成为一个函数,并调用该函数
  • you never instantiate an object at the module's level; 你永远不会在模块的层面上实例化一个对象; you shall embed it in the function, and call that function 你应该将它嵌入到函数中,并调用该函数
  • if you need an object in several different functions, create a class and encapsulate that object in that class, then use it in your functions bound to that class (so they now are called methods) 如果您需要多个不同函数中的对象,请创建一个类并将该对象封装在该类中,然后在绑定到该类的函数中使用它(因此它们现在称为方法)

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()

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? 是唯一的方法,使用一大堆函数调用单个文件,然后导入15个其他文件?

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. 所以忘记文件,并考虑包含对象的模块。

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. 最后,我向你学习Python的最好的建议就是忘却所有的习惯和使用,同时编码PHP你有,再学习这些东西,是不同的。 In the end, that can only make you a better software engineer. 最后,这只会让你成为一名更好的软件工程师。

I guess I understand what you are tring to say and to do. 我想我明白你要说什么和做什么。

Here is the random include example from PHP: 这是来自PHP的随机包含示例:

File #1 - vars.php 文件#1 - vars.php

<?php

$color = 'green';
$fruit = 'apple';

?>

File #2 - main.php 文件#2 - main.php

<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>

The fist echo command will print just "A" string, for it does not have any values assigned to the vars. 第一个echo命令将只打印“A”字符串,因为它没有分配给变量的任何值。 The next echo will print a full string thanks to your include before it. 下一个echo将打印一个完整的字符串,这要归功于它之前的include。

Python's "import", however, imports a module or it's part, so you could work with it in your current module. 但是,Python的“导入”会导入模块或它的一部分,因此您可以在当前模块中使用它。

Here is a python example: 这是一个python示例:

File 1 - echo.py 文件1 - echo.py

    apple = 'apple'
    color = 'green'

File 2 - main.py 文件2 - main.py

import echo

def func():
    print "A "+echo.color+" "+echo.fruit

if __name__ == '__main__':
    func()

In other words - you import some functionality from one module and then use it in your other module. 换句话说 - 您从一个模块导入一些功能,然后在其他模块中使用它。

The example above is not really good from programming standarts or best practises, but I think it gives you some understanding. 上面的例子在编程标准或最佳实践方面并不是很好,但我认为它可以让你有所了解。

Interesting question. 有趣的问题。 As you know, in PHP, you can separate your code by using include, which literally takes all the code in the included file and puts it wherever you called include. 如您所知,在PHP中,您可以使用include来分隔您的代码,include会直接获取所包含文件中的所有代码并将其放在您调用include的任何位置。 This is convenient for writing web applications because you can easily divide a page into parts (such as header, navigation, footer, etc). 这对于编写Web应用程序很方便,因为您可以轻松地将页面分成多个部分(例如页眉,导航,页脚等)。

Python, on the other hand, is used for way more than just web applications. 另一方面,Python不仅仅用于Web应用程序。 To reuse code, you must rely on functions or good old object-oriented programming. 要重用代码,您必须依赖函数或良好的旧的面向对象编程。 PHP also has functions and object-oriented programming FYI. PHP还具有FYI的功能和面向对象的编程。

You write functions and classes in a file and import it in another file. 您在文件中编写函数和类,并将其导入另一个文件。 This lets you access the functions or use the classes you defined in the other file. 这使您可以访问这些函数或使用您在其他文件中定义的类。

Lets say you have a function called foo in file file1.py . 假设你在文件file1.py有一个名为foo的函数。 From file2.py , you can write import file1 . file2.py ,您可以编写import file1 Then, call foo with file1.foo() . 然后,使用file1.foo()调用foo。 Alternatively, write from file1 import foo and then you can call foo with foo() . 或者, from file1 import foo写入,然后可以使用foo()调用foo。 Note that the from lets you call foo directly. 请注意, from允许您直接调用foo。 For more info, look at the python docs. 有关更多信息,请查看python文档。

On a technical level, a Python import is very similar to a PHP require, as it will execute the imported file. 在技​​术层面上,Python导入与PHP需求非常相似,因为它将执行导入的文件。 But since Python isn't designed to ultimately generate an HTML file, the way you use it is very different. 但由于Python不是为最终生成HTML文件而设计的,因此使用它的方式非常不同。

Typically a Python file will on the module level not include much executable code at all, but definitions of functions and classes. 通常,Python文件在模块级别上根本不包含很多可执行代码,而是包含函数和类的定义。 You them import them and use them as a library. 您可以导入它们并将它们用作库。

Hence having things like header() and footer() makes no sense in Python. 因此,在Python中使用header()和footer()之类的东西是没有意义的。 Those are just functions. 这些只是功能。 Call them like that, and the result they generate will be ignored. 像这样调用它们,它们生成的结果将被忽略。

So how do you split up your Python code? 那么你如何拆分你的Python代码呢? Well, you split it up into functions and classes, which you put into different files, and then import. 好吧,你将它分成函数和类,然后将它们放入不同的文件中,然后导入。

There's an execfile() function which does something vaguely comparable with PHP's include , here, but it's almost certainly something you don't want to do. 有一个execfile()函数可以做一些与PHP的include模糊的东西,在这里,但它几乎肯定是你不想做的事情。 As others have said, it's just a different model and a different programming need in Python. 正如其他人所说,它只是一个不同的模型和Python中不同的编程需求。 Your code is going to go from function to function, and it doesn't really make a difference in which order you put them, as long as they're in an order where you define things before you use them. 你的代码将从一个函数变为另一个函数,只要它们处于你使用它们之前定义的顺序,它就不会对你放置它们的顺序产生影响。 You're just not trying to end up with some kind of ordered document like you typically are with PHP, so the need isn't there. 您只是不想像某些有序文档那样通常使用PHP,因此不需要。

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

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