Sunday 15 June 2014

Little confused with import python -


I came from a PHP (as well as a bunch of other stuff) and I'm playing with Python. In PHP, when I want to include another file, I just need to type include or and the file contains everything.

But the recommended ways to do stuff in Python is by file import but it seems that the library and accessories are also included? How do you separate your code into multiple files? Is this the only way to do this, to keep a file with a whole bunch of functions call and then import 15 other files?

Things are completely different between PHP and Python, and there are several reasons for this.

But it seems that there is a way to stuff stuff in Python from File Import but it seems that more to include libraries and accessories?

In fact, the import statement is for importing objects from other modules to the existing module. You can either import existing objects to import all of the imported modules You can select:

  import foo print foo.bar   

or whatever you want module:

  foo import bar   

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

  & gt; & Gt; Foo1 & gt; & Gt; Import foo Foo2 and gt; & Gt; Import as foo How can you separate your code into multiple files?   

You have to think about your code ... This is called software design, and here are some rules:

  • At the module level Never write an algorithm; Instead, they make a function, and call that function
  • You do not instantize an object at the module level; You can embed it in the function, and call that function
  • If you need an object in many objects, then create a class and capture that object in that object, then it Divide that function into your function (now called methods)

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

      if __name__ = "__main__":   

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

      If __name__ == "__main__": main ()   
    < P> Is the only way to do this, to keep a file with a whole bunch of functions call and then import 15 other files?

    This is not the only way to do this, but it is best to do this. You make all your algorithms in the functions and object's libraries, and then you can do so in other libraries. Import Absolutely Similarly you make a complete universe of reusable code and never change the wheel again! So forget about files and think about modules with objects.

    Finally, you have my best advice for learning Python for every habit and use during PDP coding, and learn those things again, differently -Apart. Finally, it can only make you a better software engineer.

No comments:

Post a Comment