1

I am a beginner, so I apologize for this question.

When importing in Python, if I do from star import triangle, am I importing the entire star module, or just the triangle function inside the star module?

As in, when I run my script, what does python do when it looks at that line?

Also, what is happening when someone does from . import square?

I think I saw someone use from ... import square also. What's that also?

Daniel
  • 59
  • 1
  • 6

1 Answers1

1

In question order :

  • You are importing only the triangle object (function, class...) from your star module

  • Python runs the defintion, checking that there is no "typographic" error (doesn't mean there will be no runtime error later, as scripting language are so versatile, one cannot predict the valididy of a certain context early on, like in compiled language) If this is done without issues, then you have an object "triangle" that contains your definition. In the same exact way doing triangle = 2 you have an object containing an int instance with a value of 2. In python, everything is an object, with methods and properties. You can alwas check the methods of an object doing

    dir(triangle) 
    

    for example. Doing dir(triangle) with triangle being an int, we see that there is a .real method, and other ones. Doing dir(triangle) you would see the methods that your function of class implements. (method enclosed in __underscores__ are default methods, created without the need for you declaring them. They can be overloaded anyway if you want to.

  • from . import x means it imports the module after the "import" keyword, searching in the parent directory (one directory above the one your file is currentely being executed, with vary if you are in your "main" or in a module being imported)

Osamoele
  • 371
  • 4
  • 15
  • 1
    Does the code run any faster when you import less stuff? As in, if I import 100 modules, but don't actually use 90 of them, will my code run faster when I don't import those 90? Or is the speed not affected that much? – Daniel Jul 25 '21 at 09:41
  • 1
    Yes, but only because the checking part was only done on the 10 ones at import instead of the full 100 ones. Then, wether you imported 10 or 100 won't change the execution speed if you use only 10 in either case. But it will cost you RAM space as the object definition is stored there, like any other variable. In the same way, if you ever export your code as a standalone executeable with pyinstaller, doing more import means heavier file. – Osamoele Jul 25 '21 at 09:44
  • 1
    Ah, the RAM space is affected. So, if I am understanding you correctly, a benefit of `from star import triangle` is that it will cost me less RAM space and result in a smaller size executable than `import star`. Does that sound right to you? – Daniel Jul 25 '21 at 09:49
  • 1
    Exactly. And you have less "loading time" at fisrt start of your code (would it be an user interface for example) as the import is only done on a fraction of the module selected. Hence quicker, for large modules like numpy for example. – Osamoele Jul 25 '21 at 09:52