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. 7 1933
"Sean Ross" <sr***@connectm ail.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
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.String IO()
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.
michael <sp*******@btin ternet.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.String IO()
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.String IO 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
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.ja va
# 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******@telusp lanet.net
"Our analysis begins with two outrageous benchmarks."
-- "Implementa tion strategies for continuations", Clinger et al.
"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>
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('Stri ngIO')
# 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,
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('Stri ngIO')
# 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, This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
class Foo
{
public:
void somePublicMemberFunction();
protected:
|
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, looking at the MFC class 'CLink' it
|
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 id=tag200\top\level_1\level_2>Blah...</div>
Basically I am trying to understand if the slash character influences
the resulting DOM in some way. I can see where *maybe* the slashes
might create a hierarchy of some sort. It could also just be a naming
|
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... (both are "Pascal Case" with no
leading or trailing qualifiers).
For example... I'll be modelling something, e.g. a computer, and I'll
|
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 Naming Convention". It's a bit of a
long post, but I've spent the past few days on perfecting this, finally came
to the conclusion that maybe I went a bit overboard with it, almost willing
to just say "the @#$ with it" but I didn't want to let it...
| |
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?
public class MyClass
private _variableName as integer
public property VariableName as integer
|
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. You may indicate that your software works in
conjunction with PHP by saying "Foo for PHP" instead of calling
it "PHP Foo" or "phpfoo"
As the author of something released under the PHP license, can I, as I
see fit, just grant permission to an...
|
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 any comments.
--
|
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 programmatically unique, I
decided to include the date that the log was written as a part of it's
filename. I was looking through all of the date stuff and all the posts
about formatting and was wondering how in the world I was going to get all
of that...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |