defining class functions

A

Astan Chee

Hi,
I have two classes in python that are in two different files/python
scripts. Class A uses Class B like this:
class B(object):
def function1(self,something):
pass
def function2(self,something):
print "hello one"
print something

class A(object):
def __init__(self):
instance = B()
instance.function2("hello two")
self.function3()
def function3(self):
print "hello three"

What I want to do here is to (re)define function1 from function3. Is
that possible? Is there any way of redefining a function of another
class without inheriting it? Does this make sense?
Thanks
Astan
 
J

James Stroud

Astan said:
Hi,
I have two classes in python that are in two different files/python
scripts. Class A uses Class B like this:
class B(object):
def function1(self,something):
pass
def function2(self,something):
print "hello one"
print something

class A(object):
def __init__(self):
instance = B()
instance.function2("hello two")
self.function3()
def function3(self):
print "hello three"

What I want to do here is to (re)define function1 from function3. Is
that possible? Is there any way of redefining a function of another
class without inheriting it? Does this make sense?

I know what you mean, but it doesn't make a lot of sense.

Best practice is to make the function module level, especially if self
is not referenced beyond the function definition. If self is referenced,
then all selves must share the same behavior if not the same heredity,
and using a module level function is still best:

def function(quacker):
print "hello there"
quacker.quack()

James
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top