7

警告:我已经学习了 10 分钟的 Python,所以对任何愚蠢的问题表示歉意!

我编写了以下代码,但是出现以下异常:

消息文件名称行位置追溯节点 31 异常。类型错误:此构造函数不接受任何参数

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)

有什么想法吗?

4

8 回答 8

36

我将假设您来自 Java-ish 背景,因此需要指出一些关键差异。

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 "-----------------------------------------------------"
于 2008-11-23T17:09:12.270 回答
5

Python 中的构造函数称为__init__. 您还必须使用“self”作为类中所有方法的第一个参数,并使用它来设置类中的实例变量。

class Computer:

    def __init__(self, compName = "Computer1", compIp = "0.0.0.0", compScreenSize = 22):
        self.name = compName
        self.ip = compIp
        self.screenSize = compScreenSize

        self.printStats()

    def printStats(self):
        print "Computer Statistics: --------------------------------"
        print "Name:", self.name
        print "IP:", self.ip
        print "ScreenSize:", self.screenSize
        print "-----------------------------------------------------"


comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
于 2008-11-23T16:44:22.493 回答
4

老兄给自己买一本python书。潜入 Python 非常好。

于 2008-11-23T17:31:44.097 回答
2

首先,看这里

于 2008-11-23T16:46:08.260 回答
2

有几点需要指出:

  1. Python 中的所有实例方法都有一个显式的 self 参数。
  2. 构造函数被调用__init__
  3. 您不能重载方法。您可以通过使用默认方法参数来实现类似的效果。

C++:

class comp  {
  std::string m_name;
  foo(std::string name);
};

foo::foo(std::string name) : m_name(name) {}

Python:

class comp:
  def __init__(self, name=None):
    if name: self.name = name
    else: self.name = 'defaultName'
于 2008-11-23T16:48:45.650 回答
1

那不是有效的python。

Python 类的构造函数是def __init__(self, ...):,你不能重载它。

您可以做的是使用参数的默认值,例如。

class Computer:
    def __init__(self, compName="Computer1", compIp="0.0.0.0", compScreenSize=17):
        self.name = compName
        self.ip = compIp
        self.screenSize = compScreenSize

        self.printStats()

        return

    def printStats(self):
        print "Computer Statistics: --------------------------------"
        print "Name      : %s" % self.name
        print "IP        : %s" % self.ip
        print "ScreenSize: %s" % self.screenSize
        print "-----------------------------------------------------"
        return

comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
于 2008-11-23T16:46:02.253 回答
1

啊,这些是新 Python 开发人员的常见问题。

首先,应该调用构造函数:

__init__()

您的第二个问题是忘记将 self 参数包含到您的类方法中。

此外,当您定义第二个构造函数时,您将替换 Computer() 方法的定义。Python 是非常动态的,它会很高兴地让你重新定义类方法。

如果您不想使参数成为必需,则更 Pythonic 的方法可能是使用参数的默认值。

于 2008-11-23T16:49:23.247 回答
1

Python 不支持函数重载。

于 2009-01-13T16:11:29.047 回答