Placing python import statements inside a class.

A forum for general discussion of the Python programming language.

Placing python import statements inside a class.

Postby russ » Tue Apr 02, 2013 4:02 pm

I searched google and all the results I found say that imports should be placed at the top of the script which follows the PEP8 guidelines.

But what if I want to make my class completely independent of all other code so that another programmer can copy and paste the class as is into their module and it would work without having to check for the appropriate import statements at the top. Wouldn't putting imports inside the class improve encapsulation and also prevent importing unneeded libraries and code.

Also if the import statement executed with the class definition is read, wouldn't that mean it only gets executed once regardless of how many times you instantiate the class.

Consider this trivial example:
Code: Select all
import platform

class Foo(object):

    if platform.system() == "Windows":
        person = "Bill Gates"
    elif platform.system() == "Linux":
        person = "Linus Torvalds"
    else:
        person = "...tell you later..."

    hero = person

    def bar(self):
        return self.hero


...Then this one which puts the import inside the class...
Code: Select all
class Foo(object):

    import platform
    if platform.system() == "Windows":
        hero = "Bill Gates"
    elif platform.system() == "Linux":
        hero = "Linus Torvalds"
    else:
        hero = "...tell you later..."

    def bar(self):
        return self.hero


Why is the first one better?
russ
 
Posts: 18
Joined: Sat Mar 02, 2013 8:59 am

Re: Placing python import statements inside a class.

Postby Yoriz » Tue Apr 02, 2013 5:35 pm

I wouldn't cut and paste your code id expect to import your code as a module so that it would be on single import at the top of my module id use it in.
If platform was used anywhere else in your module other then inside Foo there would be no gain in import platform not being at the top of your module.
If when i import your module I'm always going to be creating an instance of Foo there would be no gain in import platform not being at the top of your module.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Placing python import statements inside a class.

Postby russ » Wed Apr 03, 2013 1:09 pm

Yoriz wrote:I wouldn't cut and paste your code id expect to import your code as a module so that it would be on single import at the top of my module id use it in.


That does make good sense admittedly.

Yoriz wrote:If platform was used anywhere else in your module other then inside Foo there would be no gain in import platform not being at the top of your module.
If when i import your module I'm always going to be creating an instance of Foo there would be no gain in import platform not being at the top of your module.


What if I did from foomodule import Foo? I tested it with the import statement at the top and it works fine but I'm not sure why. I thought when you use from it doesn't import the global scope of the module being imported from.

The other thing that is a little confusing is, if the import statement is inside the class, but not within the __init__ function, then wouldn't it only get executed once when the class definition is read, in other words wouldn't it be static, so that python wouldn't have do the import every time a new instance is created?
russ
 
Posts: 18
Joined: Sat Mar 02, 2013 8:59 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 5 guests