Hi, this is my first mail to the list so please correct me if Ive done
anything wrong.
What Im trying to figure out is a good way to organise my code. One
class per .py file is a system I like, keeps stuff apart. If I do
that, I usually name the .py file to the same as the class in it.
File: Foo.py
***********************
class Foo:
def __init__(self):
pass
def bar(self):
print 'hello world'
************************
Now, in my other module, I want to include this class. I tried these two ways: import Foo Foo.Foo.bar()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method bar() must be called with Foo instance as
first argument (got nothing instead)
Some unbound method error. Have I missunderstood something or am I on
the right track here?
I did this to, almost the same thing:
from Foo import Foo Foo.bar()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method bar() must be called with Foo instance as
first argument (got nothing instead)
One thing that I tried that worked ok was this:
import Foo instance=Foo.Foo() instance.bar()
hello world
But in my opinion, this is very ugly. Especially if the class names
are long, like my module/class TileDataBaseManager. But is this the
"right" way in python?
Another (ugly) way that Ive considered is the following. Break it out
of the class, save the functions in a file alone, import the file and
treat it like a class:
File: Foo2.py
***********************
def bar(self):
print 'hello world'
************************
import Foo2 Foo2.bar()
hello world
Very clean from the outside. I would like something like this. But,
here, I loose the __init__ function. I have to call it manually that
is, which s not good. Also, maybe the biggest drawback, its no longer
in a class. Maybe its not that important in python but from what Ive
learned (in c++) object orientation is something to strive for.
So, to sum it up, I have one class in one file, both with the same
name. How do I store/import/handle it in a nice, clean and python-like
manner?
Thank you very much in advance.
/ Alex 5 1503
On 2006-05-15, Alex <mi******@gmail.com> wrote: import Foo Foo.Foo.bar() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
...
One thing that I tried that worked ok was this: import Foo instance=Foo.Foo() instance.bar()
hello world
But in my opinion, this is very ugly. Especially if the class names
Are you sure you understand object-oriented programming? Reading the
Python tutorial[1] and re-reading the above error messages should clue
you in to what you're doing wrong. To put it simply, the whole point of
classes is that you create instances of them, each having their own
internal state, and then invoke their methods to accomplish whatever it
is that you want your program to accomplish.
1. http://docs.python.org/tut/
--
Ilkka Poutanen [ http://ipo.iki.fi/]
And unto this, Conan;
Alex wrote: Hi, this is my first mail to the list so please correct me if Ive done anything wrong.
What Im trying to figure out is a good way to organise my code. One class per .py file is a system I like, keeps stuff apart. If I do that, I usually name the .py file to the same as the class in it.
First point is that Python doesn't force you to put everything in
classes - if you just need a function, well, make it a function !-)
Also, the common pattern is to put closely related
classes/functions/constants in a same module, and closely related
modules in the same package. Since Python uses a "one file == one
module" scheme, the Javaish "one class per file" idiom leads to overly
complicated imports. And the most common naming scheme for modules is
'alllowercase'.
File: Foo.py *********************** class Foo: def __init__(self): pass def bar(self): print 'hello world'
************************
Now, in my other module, I want to include this class. I tried these two ways:
import Foo Foo.Foo.bar() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
Some unbound method error. Have I missunderstood something
Yes:
1/ you usually need to instanciate the class to call an instance method
1/ in this case, bar doesn't need to be a method, since it doesn't
depend on the instance it's called on - a plain old function would be a
better fit.
or am I on the right track here?
I did this to, almost the same thing: from Foo import Foo Foo.bar() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
One thing that I tried that worked ok was this: import Foo instance=Foo.Foo() instance.bar() hello world
But in my opinion, this is very ugly.
Nope, it's just OO at work.
Especially if the class names are long, like my module/class TileDataBaseManager. But is this the "right" way in python?
If you want to import a class from a module and create an instance of
that class, yes.
Another (ugly) way that Ive considered is the following. Break it out of the class,
Which would be a sensible thing to do given the current implementation
of bar().
save the functions in a file alone,
Nothing prevent you from putting many functions in a same module, you
know...
import the file
s/file/module/
and treat it like a class:
???
File: Foo2.py *********************** def bar(self): print 'hello world'
************************ import Foo2 Foo2.bar() hello world
You don't "treat it like a class", you're just using the normal
namespace resolution mechanism. Modules are namespaces, classes are
namespaces, objects (class instances) are namespaces, and the dot is the
lookup operator (ie : somenamespacename.somename means 'retrieve what's
actually bound to name 'somename' in namespace 'somenamespacename').
Very clean from the outside. I would like something like this. But, here, I loose the __init__ function.
Which in the given implementation is just doing nothing.
Ok, I understand that this is just example code. The rule here is:
- if you need per-instance state management, use a class (that you of
course need to instanciate - else you can't have per-instance state !-)
- if you don't need per-instance state management, use a plain function.
I have to call it manually that is, which s not good. Also, maybe the biggest drawback, its no longer in a class.
def MyFunc():
pass
print MyFunc.__class__.__name__
Python function's are instances of the function class.
Maybe its not that important in python but from what Ive learned (in c++) object orientation is something to strive for.
print "object orientation".find("class")
Being OO doesn't mean using classes. <troll>And FWIW, there quite enough
procedural Java code around to prove that using classes doesn't mean
doing OO !-) </troll>
So, to sum it up, I have one class in one file, both with the same name. How do I store/import/handle it in a nice, clean and python-like manner?
Quit the Javaish "one-class-per-file" idiom, don't bother using classes
when plain old function will do, and you'll be on track...
FWIW, Python's standard lib is open-source, so why not have a look at
the source code to see how it is organized ?
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
> Some unbound method error. Have I missunderstood something or am I on the right track here?
You misunderstood that you'd have to create an instance first before
invoking a method on that very instance. That is the same in C++, btw.
But in my opinion, this is very ugly. Especially if the class names are long, like my module/class TileDataBaseManager. But is this the "right" way in python?
I'm not sure what you mean by ugly. If you mean by ugly that you have to
instantiate an object before using it - that is the way it works in _all_
OO languages.
If you mean that you find it ugly to have long modulenames that are the same
as the class-name - yes it is ugly - but that is utterly your personal
decision to do so. In python, modules usually contain several classes, and
the module name groups them by function and has a name related to that
function. So most of the times one writes
import module
o = module.Class()
Which is not ugly - IMHO at least :) Especially not more ugly than using C++
namespaces, isn't it? And that is what a module essentially is: a
namespace.
Very clean from the outside. I would like something like this. But, here, I loose the __init__ function. I have to call it manually that is, which s not good. Also, maybe the biggest drawback, its no longer in a class. Maybe its not that important in python but from what Ive learned (in c++) object orientation is something to strive for.
You seem to have some troubles with OO in general and with the way python
does it. So I think it is kind of funny if you claim it is something to
strive for. OO is no silver bullet - if you have something that can be
dealt with using a function, it's perfectly good design to use one. Having
objects just for the sake of it introduces all sorts of problems, related
to unnecessary state, unclear implementation smeared over several methods
an the like.
So, to sum it up, I have one class in one file, both with the same name. How do I store/import/handle it in a nice, clean and python-like manner?
Group classes that belong to the same domain in one module. Import that,
possibly using an alias like
import MyLongDescriptiveModuleName as mn
don't fall for the temptation to use
from MyLongDescriptiveModuleName import *
as it will cause only more headache!
Diez
On 5/15/06, bruno at modulix <on***@xiludom.gro> wrote: Alex wrote: Hi, this is my first mail to the list so please correct me if Ive done anything wrong.
What Im trying to figure out is a good way to organise my code. One class per .py file is a system I like, keeps stuff apart. If I do that, I usually name the .py file to the same as the class in it.
First point is that Python doesn't force you to put everything in classes - if you just need a function, well, make it a function !-)
Also, the common pattern is to put closely related classes/functions/constants in a same module, and closely related modules in the same package. Since Python uses a "one file == one module" scheme, the Javaish "one class per file" idiom leads to overly complicated imports. And the most common naming scheme for modules is 'alllowercase'. File: Foo.py *********************** class Foo: def __init__(self): pass def bar(self): print 'hello world'
************************
Now, in my other module, I want to include this class. I tried these two ways:
> import Foo > Foo.Foo.bar()
Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
Some unbound method error. Have I missunderstood something
Yes: 1/ you usually need to instanciate the class to call an instance method 1/ in this case, bar doesn't need to be a method, since it doesn't depend on the instance it's called on - a plain old function would be a better fit.
or am I on the right track here?
I did this to, almost the same thing:
> from Foo import Foo > Foo.bar()
Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
One thing that I tried that worked ok was this:
> import Foo > instance=Foo.Foo() > instance.bar()
hello world
But in my opinion, this is very ugly.
Nope, it's just OO at work.
Especially if the class names are long, like my module/class TileDataBaseManager. But is this the "right" way in python?
If you want to import a class from a module and create an instance of that class, yes.
Another (ugly) way that Ive considered is the following. Break it out of the class,
Which would be a sensible thing to do given the current implementation of bar().
save the functions in a file alone,
Nothing prevent you from putting many functions in a same module, you know...
import the file
s/file/module/
and treat it like a class:
???
File: Foo2.py *********************** def bar(self): print 'hello world'
************************
> import Foo2 > Foo2.bar()
hello world
You don't "treat it like a class", you're just using the normal namespace resolution mechanism. Modules are namespaces, classes are namespaces, objects (class instances) are namespaces, and the dot is the lookup operator (ie : somenamespacename.somename means 'retrieve what's actually bound to name 'somename' in namespace 'somenamespacename').
Very clean from the outside. I would like something like this. But, here, I loose the __init__ function.
Which in the given implementation is just doing nothing.
Ok, I understand that this is just example code. The rule here is: - if you need per-instance state management, use a class (that you of course need to instanciate - else you can't have per-instance state !-) - if you don't need per-instance state management, use a plain function.
I have to call it manually that is, which s not good. Also, maybe the biggest drawback, its no longer in a class.
def MyFunc(): pass
print MyFunc.__class__.__name__
Python function's are instances of the function class.
Maybe its not that important in python but from what Ive learned (in c++) object orientation is something to strive for.
print "object orientation".find("class")
Being OO doesn't mean using classes. <troll>And FWIW, there quite enough procedural Java code around to prove that using classes doesn't mean doing OO !-) </troll>
So, to sum it up, I have one class in one file, both with the same name. How do I store/import/handle it in a nice, clean and python-like manner?
Quit the Javaish "one-class-per-file" idiom, don't bother using classes when plain old function will do, and you'll be on track...
FWIW, Python's standard lib is open-source, so why not have a look at the source code to see how it is organized ?
HTH -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in 'on***@xiludom.gro'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Thanks for taking your time to help me out. :) You have cleared out
many of my doubts. BTW, should I post "thank you" stuff here or does
it just clutter? I tried sending an email to your personal email
listed (the one from which you send that mail to the list), but I got
this error:
PERM_FAILURE: DNS Error: Domain name not found
So I guess the entire list will have to be thanked for the help. :)
Alex wrote: On 5/15/06, bruno at modulix <on***@xiludom.gro> wrote:
(snip) Thanks for taking your time to help me out. :) You have cleared out many of my doubts. BTW, should I post "thank you" stuff here
Ain't that what you just did ?-)
or does it just clutter?
Nope, 'thank you' are always welcomes - eventually, take time to snip
irrelevant material (no use to repost 100+ lines just to add 'thank you'
at the bottom).
I tried sending an email to your personal email listed (the one from which you send that mail to the list), but I got this error:
PERM_FAILURE: DNS Error: Domain name not found
hint : have a look at my signature.
So I guess the entire list will have to be thanked for the help. : )
indeed.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on***@xiludom.gro'.split('@')])" This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Brad Tilley |
last post: by
|
8 posts
views
Thread by Rob Snyder |
last post: by
|
3 posts
views
Thread by Javi |
last post: by
|
5 posts
views
Thread by M Shafaat |
last post: by
|
8 posts
views
Thread by JackRazz |
last post: by
|
11 posts
views
Thread by John Salerno |
last post: by
|
173 posts
views
Thread by Zytan |
last post: by
|
12 posts
views
Thread by Janaka Perera |
last post: by
|
5 posts
views
Thread by jyck91 |
last post: by
| | | | | | | | | | |