Please help me with class inheritance and super

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

Please help me with class inheritance and super

Postby XndrK » Fri Nov 01, 2013 4:09 pm

I don't know classes. I know the general idea of classes, but class inheritance confuses me.

For example, I'm making a program for different kinds of hats. I have a base class Hat that is defined as follows.

Code: Select all
class Hat(object):

    def __init__(self, size, kind):
        # I have absolutely no idea what I am doing. At all.
        self.size = size
        self.kind = kind

    def __repr__(self):
        return "This is a size " + str(self.size) + " " + self.kind + "."


I decide to set up a class that inherits from the Hat class called BaseballCap. (I want to make sure that the kind of hat is "baseball cap," which will come up later.) I am using Ninja IDE, and when I make BaseballCap, it slams me with this weird code.

Code: Select all
class BaseballCap(Hat):
   
    def __init__(self):
        super(BaseballCap, self).__init__()


What the heck does that super thing mean? How can I make sure that the type is "baseball cap" without rewriting the whole __init__() function and call that class without it throwing a "takes exactly 1 argument (4 given)" error? I don't know... help.

Thanks.
Last edited by Yoriz on Fri Nov 01, 2013 4:17 pm, edited 1 time in total.
Reason: Changed title
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: New #^$$%$% Classes Problem

Postby Yoriz » Fri Nov 01, 2013 4:17 pm

Super calls the parent class method you need to feed it the parameters you would fed the parent class method less 'self'.
Code: Select all
class Hat(object):

    def __init__(self, size, kind):
        # I have absolutely no idea what I am doing. At all.
        self.size = size
        self.kind = kind

    def __repr__(self):
        return "This is a size {} {}.".format(self.size, self.kind)


class BaseballCap(Hat):

    def __init__(self, size):
        super(BaseballCap, self).__init__(size, 'BaseballCap')

print BaseballCap('small')

Outputs
Code: Select all
This is a size small BaseballCap.


Heres the RTFM link to Super
Last edited by Yoriz on Fri Nov 01, 2013 4:33 pm, edited 1 time in total.
Reason: Added RTFM link to Super
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 841
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Please help me with class inheritance and super

Postby XndrK » Fri Nov 01, 2013 4:52 pm

Here's my code:

Code: Select all
class Hat(object):

    def __init__(self, size, kind):
        # I have absolutely no idea what I am doing. At all.
        self.size = size
        self.kind = kind

    def __repr__(self):
        return "This is a size %s %s." % (self.size, self.kind)

my_hat = Hat(10, "baseball cap")
print my_hat

class BaseballCap(Hat):

    def __init__(self):
        super(BaseballCap, self).__init__(size, "baseball cap")

my_baseball_cap = BaseballCap("small")
print my_baseball_cap


It throws an error that looks like this.
Code: Select all
    my_baseball_cap = BaseballCap("small")

TypeError: __init__() takes exactly 1 argument (2 given)


What made your code work and mine not? The double quotes?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Please help me with class inheritance and super

Postby Yoriz » Fri Nov 01, 2013 4:57 pm

Code: Select all
class BaseballCap(Hat):

    def __init__(self):

Your __init__ in BaseballCap does not have the ability to recieve a size parameter

Mine does
Code: Select all
class BaseballCap(Hat):

    def __init__(self, size):
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 841
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Please help me with class inheritance and super

Postby XndrK » Fri Nov 01, 2013 7:18 pm

Okay. I fixed that. New problem:

I decided to make a class Sombrero that should also have a variable brim_size. Is there a way I can use super() so I don't have to rewrite the whole __init__() block?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Please help me with class inheritance and super

Postby Yoriz » Fri Nov 01, 2013 7:55 pm

Add the new attribute to the Sombrero class __init__ and you could use super to grab the parent __repr__ text and add to it.
Code: Select all
class Sombrero(Hat):

    def __init__(self, size, brim_size):
        super(Sombrero, self).__init__(size, 'Sombrero')
        self.brim_size = brim_size

    def __repr__(self):
        existing_text = super(Sombrero, self).__repr__()
        return '{} with a {} brim.'.format(existing_text[:-1],
                                                  self.brim_size)

print Sombrero('large', 'medium')

Outputs
Code: Select all
This is a size large Sombrero with a medium brim.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 841
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Please help me with class inheritance and super

Postby XndrK » Mon Nov 04, 2013 5:17 pm

Thanks for the help.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Please help me with class inheritance and super

Postby metulburr » Mon Nov 04, 2013 6:12 pm

Code: Select all
class Hat(object):

Not sure which version you are using, but you do not need to inherit from object in python3.x. It is already using the new classes.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Please help me with class inheritance and super

Postby XndrK » Mon Nov 04, 2013 7:30 pm

I'm using 2.7. It's in my signature.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Crimson King, Keepsake, metulburr, Superiorform and 5 guests