defining class functions

Discussion in 'Python' started by Astan Chee, Jan 20, 2009.

  1. Astan Chee

    Astan Chee Guest

    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
    Astan Chee, Jan 20, 2009
    #1
    1. Advertising

  2. Astan Chee

    James Stroud Guest

    Astan Chee wrote:
    > 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
    James Stroud, Jan 20, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,570
    Victor Bazarov
    Jun 21, 2004
  2. johny smith
    Replies:
    8
    Views:
    394
    Peter Koch Larsen
    Jul 2, 2004
  3. Xiangliang Meng
    Replies:
    5
    Views:
    803
    ThosRTanner
    Apr 12, 2005
  4. Thomas
    Replies:
    4
    Views:
    158
    Thomas
    Jun 7, 2005
  5. Glenn
    Replies:
    4
    Views:
    124
    Brian Candler
    Jan 28, 2009
Loading...

Share This Page