S
Steve Holden
Good grief ...Ilias said:where do I place this function...
....thus it becomes available within class "Foo" and all other Classes?
Something like a central import?
..
Good grief ...Ilias said:where do I place this function...
....thus it becomes available within class "Foo" and all other Classes?
Something like a central import?
..
Ilias said:I like to add a method "writeDebug(self, msg)" to all (or the most
possible) classes in the system.
How do I do this?
* with new style classes
* with old style classes
MonkeeSage said:Ilias said:where do I place this function...
The place where you want it to be.
...thus it becomes available within class "Foo" and all other Classes?
Anything defined in the top-level (i.e., the sys.modules['__main__']
namespace) is accessible in every scope...but I assume you already know
that.
....You could also use a super-duper super class from which to derive all
your other classes, and add/replace any methods you want there:
class lazaridis(object):
That would probably be the most logical thing to do.
But again, I assume you already know all this, so why are you asking?
Is this part of the evaluation process?
Ilias said:no, I don't know it.
how do I define something into the top-level namespace? I assume I
could place it into the root-package of my project, into the __init__
function.
But how do I place it within my computers python installation (the big
init)?
I am aware of this technique.
But I want to modify existent classes, without touching their code.
I've noticed some interesting code on you website:
"
class file(file):
def reopen(self, name=None, mode='w', bufsize=None):
...
fh = file('test.txt', 'rb')
print fh # <open file 'test.txt', mode 'rb' at 0xb7c92814>
fh.reopen(mode='wb')
"
http://rightfootin.blogspot.com/2006/09/ruby-reopen.html
does this mean that I can add a method to a class in a similar way with
ruby? (by using class class-name(class-name): )
but the limitation is that I cannot do this with the python build-in
types?:
http://rightfootin.blogspot.com/2006/08/of-rocks-and-reptiles.html
MonkeeSage said:OK...so how do you evaluate a language when you don't know its basic
operations? Hmmm, sounds fishy.
When you just say:
def blah(): pass
Now 'blah' function is in the top-level namespace, just like global
variables. Or if it's in a different file, you'd say 'from blah import
*'. You honestly don't know this?!??
The only way to do this is to explicitly subclass the existent classes
with your own class and modify what you want there in your subclass
(see below), or use multiple inheritence as I suggested previously.
Basically, yes. In ruby you can reopen base classes; in python you can
get the same effect by subclassing the base classes with the same name
as the base class, then instantiating all your objects as that class.
This is the exact same idea as a "super-duper super class" as I
mentioned above.
You can subclass buit-in types using the same name as the parent class.
.... (many code I'll review at a later point)In fact here is what I use:
## my little library to make python more OO
## i.e., to get rid of the top-level junk...
## or at least to hide it behind some pretty
## object attributes
##
## YMMV - and don't complain if you don't
## like it; I wrote it for ME, not you
##
## Jordan Callicoat < (e-mail address removed) >
## some global methods so we can use them
## to set up the class methods
It's not the most "pythojnic" way to do things, but it works for me.
Ps. I still have a hard time belieiving that after all the time you've
spent on comp.lang.lisp, comp.lang.ruby and comp.lang.python you still
don't understand these basic concepts...if that's really true, I would
never use your consulting service!
Believe it. You are. Ain't life a bitch?Michele said:(I don't believe I am responding to a notorious troll ...)
One (bad) solution is to write in your sitecustomize.py the following:
$ echo /usr/lib/python/sitecustomize.py
import __builtin__
class Object(object):
def debug(self):
print 'some debug info'
__builtin__.object = Object
then you can do for instance
some debug info
All class inheriting from object will have the additional debug method.
However I DO NOT
RECOMMEND THIS SOLUTION FOR ANY SERIOUS WORK. For instance, it broke my
IPython installation and I am pretty sure it will broke other things
too.
But it may work for debugging purposes, if not for production use, so I
thought it was worth
posting.
George said:Short answer: you can't do it for builtin or extension types:
...
TypeError: can't set attributes of built-in/extension type 'list'
Longer asnwer: Make it a function instead of a method. This function
could try to call the respective method, and as a fallback it would
have hardcoded what to do for each supported class, something like:
def writeDebug(obj, msg):
try: return obj.writeDebug(msg)
except AttributeError:
if isinstance(obj,list):
# list msg
elif isinstance(obj,tuple):
# tuple msg
...
else:
# default object msg
If you insist though that you'd rather not use functions but only
methods, tough luck; you're better off with Ruby.
Steve said:Believe it. You are. Ain't life a bitch?
But sadly not the built-in types like int and str, which is what our
trollish friend wants to do (for some reason best known to himself).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.