python - Python 在实例化新类时返回什么?

标签 python oop class

在实例化一个新类时,我发现 Python 有很多怪癖。我敢肯定这只是因为我不习惯这种语言,但即便如此,我看到的行为确实很奇怪。

如果我打开 iPython 并输入以下内容:

class Person:
    def __init__(self, name):
        self.name = name

    def hello(self):
        print "Hello, " + self.name

一切都如我所愿:

In [2]: Person
Out[2]: <class __main__.Person at 0x1c97330>

In [3]: p = Person("Jamie")

In [4]: p
Out[4]: <__main__.Person instance at 0x1c90b98>

In [5]: p.hello()
Hello, Jamie

但是,如果我随后访问包内的一个单独的类——没什么特别的,我可能会添加——并实例化一个新类,一切都会出错。 Here's the link to the code对于 palestrina/cache.py

In [6]: from palestrina.cache import Cache

In [7]: Cache
Out[7]: <class palestrina.cache.Cache at 0x1c97750>

In [8]: c = Cache(application = 'example', backend = 'filesystem')

In [9]: c
Out[9]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/jamierumbelow/Sites/Os/palestrina/<ipython console> in <module>()

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/IPython/Prompts.pyc in __call__(self, arg)
    550 
    551             # and now call a possibly user-defined print mechanism

--> 552             manipulated_val = self.display(arg)
    553 
    554             # user display hooks can change the variable to be stored in


/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/IPython/Prompts.pyc in _display(self, arg)
    576             return IPython.generics.result_display(arg)
    577         except TryNext:
--> 578             return self.shell.hooks.result_display(arg)
    579 
    580     # Assign the default display method:


/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/IPython/hooks.pyc in __call__(self, *args, **kw)
    139             #print "prio",prio,"cmd",cmd #dbg

    140             try:
--> 141                 ret = cmd(*args, **kw)
    142                 return ret
    143             except ipapi.TryNext, exc:

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/IPython/hooks.pyc in result_display(self, arg)
    169 
    170     if self.rc.pprint:
--> 171         out = pformat(arg)
    172         if '\n' in out:
    173             # So that multi-line strings line up with the left column of


/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.pyc in pformat(self, object)
    109     def pformat(self, object):
    110         sio = _StringIO()
--> 111         self._format(object, sio, 0, 0, {}, 0)
    112         return sio.getvalue()
    113 

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.pyc in _format(self, object, stream, indent, allowance, context, level)
    127             self._readable = False
    128             return
--> 129         rep = self._repr(object, context, level - 1)
    130         typ = _type(object)
    131         sepLines = _len(rep) > (self._width - 1 - indent - allowance)

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.pyc in _repr(self, object, context, level)
    221     def _repr(self, object, context, level):
    222         repr, readable, recursive = self.format(object, context.copy(),
--> 223                                                 self._depth, level)
    224         if not readable:
    225             self._readable = False

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.pyc in format(self, object, context, maxlevels, level)
    233         and whether the object represents a recursive construct.
    234         """
--> 235         return _safe_repr(object, context, maxlevels, level)
    236 
    237 

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.pyc in _safe_repr(object, context, maxlevels, level)
    318         return format % _commajoin(components), readable, recursive
    319 
--> 320     rep = repr(object)
    321     return rep, (rep and not rep.startswith('<')), False
    322

TypeError: 'bool' object is not callable

我不明白这里发生了什么。有人可以向我解释可能发生的情况吗?

谢谢。

最佳答案

好吧,您删除了相关部分(回溯)并在您的粘贴中将其替换为 ...

但是看起来你在类的表示中有错误。

这是错误的模拟:

>>> class MyClass(object):
...     def __repr__(self):
...         return True()
...
>>> c = MyClass()
>>> c

请检查您删除的回溯,您会看到到底发生了什么。如果不能,请编辑您的问题并将其包括在内,以便我们进一步解释。

提供该类的源代码也会有所帮助。

关于python - Python 在实例化新类时返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461560/

相关文章:

Python 类属性引用

oop - 软件设计网站

python - 从小部件调用主窗口中的函数 (PySide)

python - 查找一个字符串在另一个字符串中的所有排列

python - 有没有一种方法可以将数据从表单映射到插入数据库而无需显式定义每个变量?

iphone - ScrollView 的 subview 大小为零,即使加载的文本不是零

c# - 这个自引用继承代码中发生了什么?

java - 如何找出抽象列表中的对象?

c++ - 尝试使用宽字符串创建类时出现访问冲突

python - Python-Tweepy 的设置