can a subclass reach into a superclass to call a function?

This is the place for queries that don't fit in any of the other categories.

can a subclass reach into a superclass to call a function?

Postby derrend » Thu Sep 19, 2013 1:30 am

Code: Select all
class class1():
    def function1(self):
        print "function 1"
    def function2(self):
        print "function 2"

class class2(class1):
    def function1(self):
        class1.function1(self)
        print "function 1 class2"

def main():
    object1 = class1()
    object1.function1()

    object2 = class2()
    object2.function1()
    object2.function2()
#    object2.class1.function1(self)

main()

In the above example 'object2' is able to call the 'function1' method from its super class because it is part of the 'function1' method inside 'class2'.
What i would like to know is if it is possible for 'object2' to call 'function1' from 'class1' from inside the 'main' function without making it an explicit method first.
I have attempted to do achieve this using the line of code which is commented out. Hopefully you will know what I'm trying to say once you've looked at it.
Is what I'm trying to do possible or is this impossible for security issues I don't fully understand yet? (novice programmer here)

Thanks in advance :)
derrend
 
Posts: 4
Joined: Thu Sep 19, 2013 1:05 am

Re: can a subclass reach into a superclass to call a functio

Postby micseydel » Thu Sep 19, 2013 5:50 am

I'm not sure what you want here. It's the same method, and there's no self in main(). Also, object orientation differentiates between "is-a" and "has-a" relationships. Here, you have an is-a. Has-a is where dot notation comes into play.

If you can elaborate more one what it is you're trying to achieve that you otherwise can't, we can probably give you better instruction.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1118
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: can a subclass reach into a superclass to call a functio

Postby derrend » Thu Sep 19, 2013 11:33 am

micseydel wrote:If you can elaborate more one what it is you're trying to achieve that you otherwise can't, we can probably give you better instruction.

Thanks for taking the time to reply :)

What I'm trying to do is:-
Code: Select all
class class1():
    def function1(self):
        print "function 1"
    def function2(self):    <-- Access this method
        print "function 2"

from inside of the 'main' function
Code: Select all
class class2(class1):
    def function1(self):
        class1.function1(self)    <-- Without having to specify it here
        print "function 1 class2"

using an object instantiated from 'class2'.
My thinking is to move the code from 'class2' and specify it in the 'main' function.

Hope this helps :)

PS to the administrators, It would be nice to be able to edit my original post for further clarification if needed ;)
derrend
 
Posts: 4
Joined: Thu Sep 19, 2013 1:05 am

Re: can a subclass reach into a superclass to call a functio

Postby stranac » Thu Sep 19, 2013 1:02 pm

If you want to use a method of class1 with an object of class2, you should not be overwriting that method at all.
Having the shared code as a separate function is also a possibility.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: can a subclass reach into a superclass to call a functio

Postby derrend » Thu Sep 19, 2013 1:18 pm

stranac wrote:Having the shared code as a separate function is also a possibility.

Thanks for the reply

Yes I thought about that too, I'm not actually trying to achieve anything here just understand the language better.
So just to clarify, an object cannot gain access to a method if is has been overwritten by its own class? Is that right??

Sorry for the vagueness, this is harder to put into plain english than i thought :p
derrend
 
Posts: 4
Joined: Thu Sep 19, 2013 1:05 am

Re: can a subclass reach into a superclass to call a functio

Postby stranac » Thu Sep 19, 2013 1:48 pm

It can, but there is rarely a good reason to do that.
You can mess around with __class__, __bases__ and the likes, but there are usually better ways to do stuff.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: can a subclass reach into a superclass to call a functio

Postby derrend » Thu Sep 19, 2013 2:28 pm

stranac wrote:You can mess around with __class__, __bases__ and the likes.

Thanks for the info I will definitely check those out :)
derrend
 
Posts: 4
Joined: Thu Sep 19, 2013 1:05 am


Return to General Coding Help

Who is online

Users browsing this forum: micseydel and 4 guests