473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Where to "import"?

Hi!

If I have two files .py such as

m.py
from c import *
...
x=c()
...
os.any_method ...
...

c.py
class c:
def __init__(self, ...):
...
os.any_method ...
...
...

both using os module where should I put the "import os"? In both files?

Thanks
Paulo
Mar 8 '07 #1
14 2101

"Paulo da Silva" <ps********@esotericaX.ptXwrote in message
news:11***************@iceman.esoterica.pt...
| Hi!
|
| If I have two files .py such as
|
| m.py
| from c import *
| ...
| x=c()
| ...
| os.any_method ...
| ...
|
| c.py
| class c:
| def __init__(self, ...):
| ...
| os.any_method ...
| ...
| ...
|
| both using os module where should I put the "import os"? In both files?

I would even though having it in m.py is not *currently* necessary. Having
the import of os into m.py depend on the way you write and import c.py
looks fragile, and it certainly is a memory burden to remember that c does
so. Also, I probably would use 'import c' instead.

tjr

Mar 8 '07 #2
Paulo da Silva a écrit :
Hi!

If I have two files .py such as

m.py
from c import *
avoid this kind of import except in an interactive interpreter and
eventually in a package __init__.py. Better to use either:

from c import c
or
import c
...
x = c.c()
...
x=c()
...
os.any_method ...
Then you need to import os
...

c.py
class c:
class C(object):

1/ better to stick to naming conventions (class names in CamelCase)
2/ do yourself a favor: use new-style classes
def __init__(self, ...):
...
os.any_method ...
...
...

both using os module where should I put the "import os"? In both files?
Yes. In your above example, it worked because of the "from c import *" -
and this is exactly why it's bad form to use this in a module (well:
that's one of the reasons why). Each module should *explicitly* import
all it's direct dependencies.

Mar 8 '07 #3
both using os module where should I put the "import os"? In both files?

You don't really have a choice, you have to import os in both files.
Python will only load the os module into memory once, but the import
statements are needed to add the os module to the c and m module
namespaces. The code in c.py cannot 'see' the code in m.py, even if it
was imported by m.py. The import command is not like #include in C, it
is not a code dump.

-m

Mar 8 '07 #4
Paulo da Silva <ps********@esotericaX.ptXwrites:
Hi!

If I have two files .py such as

m.py
from c import *
Best done as either

import c # then use 'x = c.c()'

or

from c import c

Using 'from foo import *' leads to names appearing in your current
namespace that are difficult to track to their origin by reading the
source code.
both using os module where should I put the "import os"? In both
files?
Yes. "import os" does two things of note:

- iff the module is not currently loaded and executed, do so
- bind the module object to the name "os" in the current namespace

The first step means that you're not losing anything by importing the
module wherever it's needed. The second means the module is available
for use within the current namespace.

--
\ "When cryptography is outlawed, bayl bhgynjf jvyy unir |
`\ cevinpl." -- Anonymous |
_o__) |
Ben Finney

Mar 8 '07 #5
Paulo da Silva escreveu:
This is to thank all the answers I got so far.
Now I understand perfectly how "import" works.

Regards.
Paulo
Mar 8 '07 #6
Paulo da Silva escreveu:
This is to thank all the answers I got so far.
Now I understand perfectly how "import" works.

Regards.
Paulo
Mar 8 '07 #7
Bruno Desthuilliers escreveu:
Paulo da Silva a écrit :
....
>>
c.py
class c:
class C(object):

1/ better to stick to naming conventions (class names in CamelCase)
Ok. Thanks.
2/ do yourself a favor: use new-style classes
I still have python 2.4.3 (The gentoo current version).
I didn't find this in my tutorial. Would you please enlight me
a bit about this (class C(object))? Or just point me out some doc
to read about.

Thank you.
Paulo
Mar 8 '07 #8
Paulo da Silva a écrit :
Bruno Desthuilliers escreveu:
>>Paulo da Silva a écrit :

...

>>>c.py
class c:

class C(object):

1/ better to stick to naming conventions (class names in CamelCase)

Ok. Thanks.
FWIW:
http://www.python.org/dev/peps/pep-0008/
>>2/ do yourself a favor: use new-style classes

I still have python 2.4.3 (The gentoo current version).
new-style classes came with Python 2.2.1 IIRC.
I didn't find this in my tutorial. Would you please enlight me
a bit about this (class C(object))?
Inheriting from the builtin class 'object' (or from any other new-style
class) makes your class a new-style class. new-style classes have much
more features than old-style ones. Like support for properties (computed
attributes). old-style classes are still here mainly for compatibility
with old code. While there not yet officially deprecated, you can
consider them as such.
Or just point me out some doc
to read about.
http://www.python.org/doc/newstyle/

HTH
Mar 8 '07 #9
On Mar 8, 5:49 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.fr>
>1/ better to stick to naming conventions (class names in CamelCase)
Ok. Thanks.

FWIW:http://www.python.org/dev/peps/pep-0008/
By my reading, PEP8 doesn't specify CamelCase as preferred over the
other styles it mentions. non_camel_case is also considered good
style, as I understand (and is used all over the standard library).

Regards,
Jordan

Mar 9 '07 #10
Disregard my last message, I'm stupid. I totally missed that Bruno was
talking about classname. Bruno is exactly right.

Mar 9 '07 #11
En Fri, 09 Mar 2007 01:09:36 -0300, MonkeeSage <Mo********@gmail.com>
escribió:
>FWIW:http://www.python.org/dev/peps/pep-0008/

By my reading, PEP8 doesn't specify CamelCase as preferred over the
other styles it mentions. non_camel_case is also considered good
style, as I understand (and is used all over the standard library).
Class names should be CamelCase. Read it again, and notice the difference
between a "Descriptive" section and a "Prescriptive" one.

--
Gabriel Genellina

Mar 9 '07 #12
On Mar 8, 10:27 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
Class names should be CamelCase. Read it again, and notice the difference
between a "Descriptive" section and a "Prescriptive" one.
Yes, I misread Bruno's comment (missed that he was speaking of class
names). Disregard my post.

Mar 9 '07 #13
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
Class names should be CamelCase.
Note that the term "camel case" has ambiguous usage. Some use it to
refer to *both* of "nameWithSeveralWords" and "NameWithSeveralWords".

I prefer to use the term "title case" to refer unambiguously to
"NameWithSeveralWords", leaving the term "camel case" to describe the
case with the humps only in the middle :-)

PEP 8 recommends TitleCase for class names. The use of camelCase for
instances and method names seems to come from (at least) Java, but PEP
8 prefers lowercase_with_underscores.

--
\ "My theory of evolution is that Darwin was adopted." -- Steven |
`\ Wright |
_o__) |
Ben Finney

Mar 9 '07 #14
Ben Finney wrote:
I prefer to use the term "title case" to refer unambiguously to
"NameWithSeveralWords", leaving the term "camel case" to describe the
case with the humps only in the middle :-)
The names "TitleCase" and "camelCase" might suffice here.

--
--Scott David Daniels
sc***********@acm.org
Mar 9 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
2
by: Torsten Mohr | last post by:
Hi, i tried to find the file and line in the C sources of python where the command "import" is implemented. Can anybody give me some hint on this? Thanks, Torsten.
2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
5
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.