question about imports in a class

Diez B. Roggisch deets at nospam.web.de
Mon Dec 7 17:44:20 EST 2009


J schrieb:
> On Mon, Dec 7, 2009 at 16:57, Diez B. Roggisch <deets at nospam.web.de> wrote:
> 
>>> if I put the import at the beginning of the class, it just dawned on
>>> me that perhaps I still have to explicitly call the function by class:
>>>
>>> sysinfo.py
>>>
>>> class myclass():
>>>    import os
>>>    def findDMIDecode(self):
>>>        for r,d,f in myclass.os.walk(args)
>>>
>>> is that correct?
>> It is correct in the sense that it works, but it is by no means good
>> practice.
> 
> So good practice (I'm doing this for real as well as for my own
> education) would be:
> 
> sysinfo.py:
> 
> import os
> 
> class ClassA():
>     sysinfo.os.walk()
> 
> class ClassB():
>     sysinfo.os.walk()

No, no sysinfo. Just


import os

class A():

    def foo(self):
        os.path.walk("/")

> 
> Sorry for the incredibly entry-level questions...  I'm a more senior
> member on a few Linux related lists/groups, and I can certainly
> understand if questions as basic as these are annoying, so I'm really
> trying to not ask them unless I'm just really confused about
> something.
> 
> OTOH, if there's something more akin to a python newbies kind of list
> where this kind of basic thing is more appropriate, please let me
> know, because really, I don't want to clutter up python-list with
> stuff that would be better off elsewhere.

I'm not annoyed. I just wonder from what you got the impression that 
import statements are something that belongs inside functions - even if 
python allows it.

I don't know of any other language (I don't do ruby & perl) that allows 
this, C/C++, java, Pascal, Haskell, ML - all of them declare their 
intended use of a library on the beginning of a file.

You seem to really be really determined to place them everywhere else...




Diez



More information about the Python-list mailing list