473,387 Members | 1,493 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,387 software developers and data experts.

Question regarding naming convention

Hello,

Apologies if this seems like a trivial question, but it would help me
with my python, coming from a Java background.

What is the preferred naming convention for a .py file, that contains a
class ? For example, if I have one class (MyClass), I wouldn't really
want to put it in myclass.py, as I'd end up referencing myclass.MyClass
(unless I use the import-from which I'm trying to avoid).

I'm also not sure about putting several unrelated classes in the same
..py file - It could just be Java talking, but it seems odd.

Thank you for any ideas.

Michael.

Jul 18 '05 #1
7 1919
"Sean Ross" <sr***@connectmail.carleton.ca> writes:
http://www.python.org/doc/essays/styleguide.html#names
<quote> [...] "Packages" (groups of modules, supported by the "ni" module) generally have
a short all lowercase name.
</quote>

[...]

Also note the 'ni' module is long gone, replaced by what is now the
standard import mechanism.
John
Jul 18 '05 #2
Sean Ross wrote:
http://www.python.org/doc/essays/styleguide.html#names


<snip quote>

Yeah, thanks for the quote. Unfortunately, this still leaves me with
syntax that's a bit unfriendly. Taking the StringIO class as an example,
I can either write:

import StringIO
s = StringIO.StringIO()

or:

from StringIO import StringIO
s = StringIO()

Both of the above seem to overcomplicate the syntax. Of the two, I
prefer the second, but I'm sure I've read on c.l.py that the
from..import.. version is not a preferred way of doing things.

Is there no way to write a class, such that the statement:

import MyClass

would dynamically import the MyClass class from MyClass.py ?

It just surprises me that there isn't a neater way around this, as
Python seems to encapsulate most everything else in a simple way.

Thanks,

Michael.

Jul 18 '05 #3
michael <sp*******@btinternet.com> writes:
[...]
Yeah, thanks for the quote. Unfortunately, this still leaves me with
syntax that's a bit unfriendly. Taking the StringIO class as an
example, I can either write:

import StringIO
s = StringIO.StringIO()

or:

from StringIO import StringIO
s = StringIO()

Both of the above seem to overcomplicate the syntax. Of the two, I
Well, one is simpler when you only use StringIO.StringIO once or
twice, and the other is simpler when you use it lots of times.
Really, there are two issues, I suppose. First, the second form has
the convenience of shorter names. Second, the first form is useful
where somebody reading your code would otherwise have to keep
referring to your imports to see where names came from, or might be
confused by similarly-named classes in different modules. Third, ease
of switching names -- sometimes it's convenient to be able to swap

from StringIO import StringIO

to

from cStringIO import StringIO

And have your code work unchanged. OK, three issues.

A fourth issue is that it's nice not to mix the two styles, to avoid
confusing readers.

prefer the second, but I'm sure I've read on c.l.py that the
from..import.. version is not a preferred way of doing things.
Nothing un-preferred about 'from foo import bar'. What is discouraged
is 'from foo import *' (that's a literal *, if you haven't seen that
syntax before -- see the tutorial). It is useful sometimes, though.
In PyQt, for example.

Is there no way to write a class, such that the statement:

import MyClass

would dynamically import the MyClass class from MyClass.py ?
Well, maybe (I'm vaguely aware that there's an import hook of some
kind). *Nobody* would thank you for it, other than as a joke.

It just surprises me that there isn't a neater way around this, as
Python seems to encapsulate most everything else in a simple way.


Modules are useful, and explicit is better than implicit.
John
Jul 18 '05 #4
Quoth michael:
[...]
Is there no way to write a class, such that the statement:

import MyClass

would dynamically import the MyClass class from MyClass.py ?
Not recommended, but:

# MyClass.py
import sys
class MyClass(object):
pass
sys.modules['MyClass'] = MyClass

This is a dangerous hack; I'm sure there's lots of code which
expects sys.modules to contain only modules.

Better, if you really want this kind of behaviour, is to write a
custom __import__ function. See
<http://www.python.org/doc/current/lib/built-in-funcs.html>
It just surprises me that there isn't a neater way around this, as
Python seems to encapsulate most everything else in a simple way.


It's fairly rare for a module to contain only one entity of
interest to importers. (StringIO is unusual in this respect.)

Since you're coming from a Java background, you might try thinking
of modules as analogous to leaf-level Java packages. For example,
where Java has
java/
util/
LinkedList.java
AbstractList.java
# etc.
Python would have
java/
__init__.py # to make java a package; probably just has docstring
util.py # contains classes LinkedList, AbstractList, etc.

--
Steven Taschuk st******@telusplanet.net
"Our analysis begins with two outrageous benchmarks."
-- "Implementation strategies for continuations", Clinger et al.

Jul 18 '05 #5
"michael" wrote:
Both of the above seem to overcomplicate the syntax. Of the two,
I prefer the second, but I'm sure I've read on c.l.py that the from.
import.. version is not a preferred way of doing things.


"from ... import *" is usually a bad idea.

"from SomeClass import SomeClass" is an excellent idea.

more here:

http://effbot.org/zone/import-confusion.htm

</F>


Jul 18 '05 #6
michael wrote:
Both of the above seem to overcomplicate the syntax. Of the two, I
prefer the second, but I'm sure I've read on c.l.py that the
from..import.. version is not a preferred way of doing things.

Is there no way to write a class, such that the statement:

import MyClass

would dynamically import the MyClass class from MyClass.py ?

It just surprises me that there isn't a neater way around this, as
Python seems to encapsulate most everything else in a simple way.


Not sure if this is what you're after, but:
def importobj(name): mod = __import__(name)
obj = getattr(mod, name)
globals()[name] = obj

# import the StringIO object from the StringIO module importobj('StringIO')
# is it in the global namespace? yes: dir()

['StringIO', '__builtins__', '__doc__', '__name__', 'importobj']

Putting something into globals() is a bit of a kludge, though. You're probably
better off using from x import x.

Cheers,

Jul 18 '05 #7
michael wrote:
Both of the above seem to overcomplicate the syntax. Of the two, I
prefer the second, but I'm sure I've read on c.l.py that the
from..import.. version is not a preferred way of doing things.

Is there no way to write a class, such that the statement:

import MyClass

would dynamically import the MyClass class from MyClass.py ?

It just surprises me that there isn't a neater way around this, as
Python seems to encapsulate most everything else in a simple way.


Not sure if this is what you're after, but:
def importobj(name): mod = __import__(name)
obj = getattr(mod, name)
globals()[name] = obj

# import the StringIO object from the StringIO module importobj('StringIO')
# is it in the global namespace? yes: dir()

['StringIO', '__builtins__', '__doc__', '__name__', 'importobj']

Putting something into globals() is a bit of a kludge, though. You're probably
better off using from x import x.

Cheers,

Jul 18 '05 #8

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

Similar topics

27
by: Derek | last post by:
The company where I work uses a naming convention that I have never used before. They use mixed-case letters for public member functions, but lower-case with underscores for the rest, like this:...
7
by: Iguana | last post by:
Say you've got this class.... <template TYPE> class CNode { TYPE data; }; How would you initialise 'data'? I have to admitt I've not been programming as much as I did when I was younger,...
3
by: Brian | last post by:
Hello: I saw the following the other day in the id attribute of a div tag and was wondering if it had any special meaning. Here is the example: <div...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
0
by: Carl Colijn | last post by:
Hi all, Disclaimer: before I might trigger your "let's start a holy war!" button, I'd like to say I'm not intended to; I just post this message to get some input and not to promote "Yet Another...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
2
by: yawnmoth | last post by:
The PHP license states the following: 4. Products derived from this software may not be called "PHP", nor may "PHP" appear in their name, without prior written permission from group@php.net. ...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
16
by: Bruce W. Darby | last post by:
I've almost completed my little application for work. This weekend I've been working on Streams so I can write a logfile showing the work that was accomplished. Wanting to make each logfile...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.