Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Problem with overloaded constructors

WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions!

I have written the following code, however I get the following exception:

Message File Name Line Position Traceback Node 31 exceptions.TypeError: this constructor takes no arguments

class Computer:

    name = "Computer1"
    ip = "0.0.0.0"
    screenSize = 17


    def Computer(compName, compIp, compScreenSize):
        name = compName
        ip = compIp
        screenSize = compScreenSize

        printStats()

        return

    def Computer():
        printStats()

        return

    def printStats():
        print "Computer Statistics: --------------------------------"
        print "Name:" + name
        print "IP:" + ip
        print "ScreenSize:" , screenSize // cannot concatenate 'str' and 'tuple' objects
        print "-----------------------------------------------------"
        return

comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)

Any thoughts?

like image 306
TK. Avatar asked Nov 23 '08 16:11

TK.


People also ask

Can you have overloaded constructors in Python?

Python does not support Constructor overloading; it has no form of function. In Python, Methods are defined solely by their name, and there can be only one method per class with a given name.

Why overloading is not supported in Python?

When the runtime encounters another function with the same name it updates the entry in the local namespace and thus removes the possibility of two functions co-existing. Hence python does not support Function overloading.

How is constructor overloading done in Python?

The constructor overloading is done by checking conditions for the arguments passed and performing required actions. For example, consider passing an argument to the class sample, If the parameter is an int, the square of the number should be the answer.

Is it possible to overload constructors?

Constructors can be overloaded in a similar way as function overloading. Overloaded constructors have the same name (name of the class) but the different number of arguments.


1 Answers

I'm going to assume you're coming from a Java-ish background, so there are a few key differences to point out.

class Computer(object):
    """Docstrings are used kind of like Javadoc to document classes and
    members.  They are the first thing inside a class or method.

    You probably want to extend object, to make it a "new-style" class.
    There are reasons for this that are a bit complex to explain."""

    # everything down here is a static variable, unlike in Java or C# where
    # declarations here are for what members a class has.  All instance
    # variables in Python are dynamic, unless you specifically tell Python
    # otherwise.
    defaultName = "belinda"
    defaultRes = (1024, 768)
    defaultIP = "192.168.5.307"

    def __init__(self, name=defaultName, resolution=defaultRes, ip=defaultIP):
        """Constructors in Python are called __init__.  Methods with names
        like __something__ often have special significance to the Python
        interpreter.

        The first argument to any class method is a reference to the current
        object, called "self" by convention.

        You can use default function arguments instead of function
        overloading."""
        self.name = name
        self.resolution = resolution
        self.ip = ip
        # and so on

    def printStats(self):
        """You could instead use a __str__(self, ...) function to return this
        string.  Then you could simply do "print(str(computer))" if you wanted
        to."""
        print "Computer Statistics: --------------------------------"
        print "Name:" + self.name
        print "IP:" + self.ip
        print "ScreenSize:" , self.resolution //cannot concatenate 'str' and 'tuple' objects
        print "-----------------------------------------------------"
like image 87
3 revs, 2 users 99% Avatar answered Sep 21 '22 18:09

3 revs, 2 users 99%