我继续得到类型错误 [英] I keep on getting a type error

查看:103
本文介绍了我继续得到类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在教自己python类,当我运行我的代码,我得到以下错误:

  class Critter (对象):
一个虚拟宠物
def _init_(self,name,mood):
print(A new critter has been born !!!!! )
self.name = name
self .__ mood = mood

def talk(self):
print(\\\
Im,self.name)
print(现在我觉得,self._mood)

def _private_method(self):
print(this is a private method)

def public(self):
print(This is a public method。)
self._private_method()

crit = Critter(name =Poochie ,mood =happy)
crit.talk()crit.public_method()

输入(按回车离开)
/ pre>

我收到错误:

  call last):
文件/Users/calebmatthias/Document/workspace/de.vogella.python.first/practice.py,第27行,在< module>
crit = Critter(name =Poochie,mood =happy)
TypeError:object .__ new __()不带参数


解决方案

我建议您更仔细地格式化提交内容。 Python对于缩进真的很挑剔 - 请阅读 PEP8 ,了解如何以正确格式化Python代码。



问题是你拼写 __ init __ 错。你有 _init _ 这只是Python的另一种方法。


Well I'm teaching myself about python classes and when I run my code I get the following error:

    class Critter(object):
        """A virtual pet"""
            def _init_(self, name, mood):
                print("A new critter has been born!!!!!")
                self.name = name
                self.__mood = mood 

           def talk(self):
           print("\n Im",self.name)
           print("Right now I feel",self._mood)

           def _private_method(self):
               print("this is a private method")

           def public(self):
               print("This is a public method. ")
               self._private_method( )

crit = Critter(name = "Poochie", mood = "happy")
crit.talk( )crit.public_method( )

input("Press enter to leave")

I receive the error:

    Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/practice.py", line   27, in <module>
    crit = Critter(name = "Poochie", mood = "happy")
TypeError: object.__new__() takes no parameters

解决方案

I would recommend that you more carefully format your submissions. Python is really picky about indentation -- read PEP8 for a good intro on how to properly format Python code.

The problem is that you spelled __init__ wrong. You have _init_ which is just another method to Python.

这篇关于我继续得到类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆