Python:类实例可以计算为任何类型吗?

Python:类实例可以计算为任何类型吗?,python,class,tuples,Python,Class,Tuples,类可以通过其_str__方法充当字符串,也可以通过其_call__方法充当函数。它可以充当列表或元组吗 class A (object): def __???__ (self): return (1, 2, 3) >>> a = A() >>> a * 3 (1, 2, 3, 1, 2, 3, 1, 2, 3) 编辑… 这里有一个更好的例子来帮助澄清上述问题 class Vector (object): def __init

类可以通过其_str__方法充当字符串,也可以通过其_call__方法充当函数。它可以充当列表或元组吗

class A (object):
    def __???__ (self):
        return (1, 2, 3)

>>> a = A()
>>> a * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
编辑…

这里有一个更好的例子来帮助澄清上述问题

class Vector (object):
    def __init__ (self):
        self.vec = (1,2,3)
    def __???__ (self):
        # something like __repr__; non-string
        return self.vec

class Widget (object):
    def __init__ (self):
        self.vector = Vector()

>>> w = Widget()
>>> w.vector
(1, 2, 3) # not a string representation (at least, before being repr'd here)

基本上,我想要像_repr_uu这样的东西,它不返回字符串,但在我简单地调用指向向量实例的名称时返回元组(或列表),但我不想失去实例中的其他功能,比如访问其他属性和方法。我也不想使用
w.vector.vec
来获取数据。我希望vector的行为类似于w的元组属性,同时仍然能够执行类似于
w.vector.which()
,或重写uuu mul\uu的操作,以便通过
w.vector*5
缩放向量。可能吗?

调用str时,该类不充当字符串。它创建并返回一个新的字符串对象。基本上,当您在对象上调用
str(something)
时,实际情况如下:

a = str(someObject)

a = someObject.__str__()
因此,
str
函数基本上可以被认为是这样做的:

def str(variable):
    return variable.__str__()
调用
list()
tuple()
set()
等时也是如此。如果我认为你的问题是正确的:

tuple()
list()
set()
都调用类的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class MyClass(object):
    ...
    def __iter__(self):
        ...
        return myIterable

调用str时,该类不充当字符串。它创建并返回一个新的字符串对象。基本上,当您在对象上调用
str(something)
时,实际情况如下:

a = str(someObject)

a = someObject.__str__()
因此,
str
函数基本上可以被认为是这样做的:

def str(variable):
    return variable.__str__()
调用
list()
tuple()
set()
等时也是如此。如果我认为你的问题是正确的:

tuple()
list()
set()
都调用类的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class MyClass(object):
    ...
    def __iter__(self):
        ...
        return myIterable

对于示例中的特定行为(
A*3
给出了
A
中数据的三个串联副本),您希望实现
\uuuu mul\uu()
运算符

例如,这些是等效的:

>>> a = [1,2,3]
>>> a*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> a.__mul__(3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 
更一般地说,如果您想要实现一个序列类型,您必须实现所有的。你必须定义-

  • A[3]
    是什么意思(
    \uuu getitem\uuuuuuuuuuuuuuuuuuuuuuuu()
    \uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
  • 什么是
    A[1:10]
    的意思(
    \uu getslice\uuu()
  • A:
中项目的
表示什么(
\uuu iter\uuu()
) 等等

以下是
list
s上定义的方法的完整列表:

>>> pprint.pprint(dict(list.__dict__))
{'__add__': <slot wrapper '__add__' of 'list' objects>,
 '__contains__': <slot wrapper '__contains__' of 'list' objects>,
 '__delitem__': <slot wrapper '__delitem__' of 'list' objects>,
 '__delslice__': <slot wrapper '__delslice__' of 'list' objects>,
 '__doc__': "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items",
 '__eq__': <slot wrapper '__eq__' of 'list' objects>,
 '__ge__': <slot wrapper '__ge__' of 'list' objects>,
 '__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>,
 '__getitem__': <method '__getitem__' of 'list' objects>,
 '__getslice__': <slot wrapper '__getslice__' of 'list' objects>,
 '__gt__': <slot wrapper '__gt__' of 'list' objects>,
 '__hash__': None,
 '__iadd__': <slot wrapper '__iadd__' of 'list' objects>,
 '__imul__': <slot wrapper '__imul__' of 'list' objects>,
 '__init__': <slot wrapper '__init__' of 'list' objects>,
 '__iter__': <slot wrapper '__iter__' of 'list' objects>,
 '__le__': <slot wrapper '__le__' of 'list' objects>,
 '__len__': <slot wrapper '__len__' of 'list' objects>,
 '__lt__': <slot wrapper '__lt__' of 'list' objects>,
 '__mul__': <slot wrapper '__mul__' of 'list' objects>,
 '__ne__': <slot wrapper '__ne__' of 'list' objects>,
 '__new__': <built-in method __new__ of type object at 0x1E1DACA8>,
 '__repr__': <slot wrapper '__repr__' of 'list' objects>,
 '__reversed__': <method '__reversed__' of 'list' objects>,
 '__rmul__': <slot wrapper '__rmul__' of 'list' objects>,
 '__setitem__': <slot wrapper '__setitem__' of 'list' objects>,
 '__setslice__': <slot wrapper '__setslice__' of 'list' objects>,
 '__sizeof__': <method '__sizeof__' of 'list' objects>,
 'append': <method 'append' of 'list' objects>,
 'count': <method 'count' of 'list' objects>,
 'extend': <method 'extend' of 'list' objects>,
 'index': <method 'index' of 'list' objects>,
 'insert': <method 'insert' of 'list' objects>,
 'pop': <method 'pop' of 'list' objects>,
 'remove': <method 'remove' of 'list' objects>,
 'reverse': <method 'reverse' of 'list' objects>,
 'sort': <method 'sort' of 'list' objects>}
>>pprint.pprint(dict(list.\uu dict)
{“添加”:,
“\uuuu包含”:,
“uuu delitem”:,
“\uuu delslice\uuuu”:,
“\uuuu doc\uuuuuu”:“列表()->新空列表\n列表(iterable)->从iterable的项初始化的新列表”,
“uuu eq”:,
""格":,,
“\uuuu getattribute\uuuuuu”:,
“\uuu getitem\uuuu”:,
“\uuu getslice\uuuu”:,
“\uuugt\uuuuuu”:,
“\uuuu散列”:无,
"iadd":,,
“伊穆尔”:,
“\uuuu init\uuuuuuu”:,
“\uuuuu iter\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
‘乐’,
“\uuuu len\uuuuuuu”:,
“————————”,
“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“\uuu ne\uuuuu”:,
"新":,,
“报告”:,
“\uuuu反向”:,
""rmul":,,
“\uuuu setitem\uuuuuuuuu”:,
“\uuuu设置许可证”:,
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“追加”:,
“计数”:,
“扩展”:,
“索引”:,
“插入”:,
“流行音乐”:,
“删除”:,
“反向”:,
'排序':}

对于示例中的特定行为(
A*3
给出了
A
中数据的三个串联副本),您希望实现
\u mul\u()
运算符

例如,这些是等效的:

>>> a = [1,2,3]
>>> a*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> a.__mul__(3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 
更一般地说,如果您想要实现一个序列类型,您必须实现所有的。你必须定义-

  • A[3]
    是什么意思(
    \uuu getitem\uuuuuuuuuuuuuuuuuuuuuuuu()
    \uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
  • 什么是
    A[1:10]
    的意思(
    \uu getslice\uuu()
  • A:
中项目的
表示什么(
\uuu iter\uuu()
) 等等

以下是
list
s上定义的方法的完整列表:

>>> pprint.pprint(dict(list.__dict__))
{'__add__': <slot wrapper '__add__' of 'list' objects>,
 '__contains__': <slot wrapper '__contains__' of 'list' objects>,
 '__delitem__': <slot wrapper '__delitem__' of 'list' objects>,
 '__delslice__': <slot wrapper '__delslice__' of 'list' objects>,
 '__doc__': "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items",
 '__eq__': <slot wrapper '__eq__' of 'list' objects>,
 '__ge__': <slot wrapper '__ge__' of 'list' objects>,
 '__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>,
 '__getitem__': <method '__getitem__' of 'list' objects>,
 '__getslice__': <slot wrapper '__getslice__' of 'list' objects>,
 '__gt__': <slot wrapper '__gt__' of 'list' objects>,
 '__hash__': None,
 '__iadd__': <slot wrapper '__iadd__' of 'list' objects>,
 '__imul__': <slot wrapper '__imul__' of 'list' objects>,
 '__init__': <slot wrapper '__init__' of 'list' objects>,
 '__iter__': <slot wrapper '__iter__' of 'list' objects>,
 '__le__': <slot wrapper '__le__' of 'list' objects>,
 '__len__': <slot wrapper '__len__' of 'list' objects>,
 '__lt__': <slot wrapper '__lt__' of 'list' objects>,
 '__mul__': <slot wrapper '__mul__' of 'list' objects>,
 '__ne__': <slot wrapper '__ne__' of 'list' objects>,
 '__new__': <built-in method __new__ of type object at 0x1E1DACA8>,
 '__repr__': <slot wrapper '__repr__' of 'list' objects>,
 '__reversed__': <method '__reversed__' of 'list' objects>,
 '__rmul__': <slot wrapper '__rmul__' of 'list' objects>,
 '__setitem__': <slot wrapper '__setitem__' of 'list' objects>,
 '__setslice__': <slot wrapper '__setslice__' of 'list' objects>,
 '__sizeof__': <method '__sizeof__' of 'list' objects>,
 'append': <method 'append' of 'list' objects>,
 'count': <method 'count' of 'list' objects>,
 'extend': <method 'extend' of 'list' objects>,
 'index': <method 'index' of 'list' objects>,
 'insert': <method 'insert' of 'list' objects>,
 'pop': <method 'pop' of 'list' objects>,
 'remove': <method 'remove' of 'list' objects>,
 'reverse': <method 'reverse' of 'list' objects>,
 'sort': <method 'sort' of 'list' objects>}
>>pprint.pprint(dict(list.\uu dict)
{“添加”:,
“\uuuu包含”:,
“uuu delitem”:,
“\uuu delslice\uuuu”:,
“\uuuu doc\uuuuuu”:“列表()->新空列表\n列表(iterable)->从iterable的项初始化的新列表”,
“uuu eq”:,
""格":,,
“\uuuu getattribute\uuuuuu”:,
“\uuu getitem\uuuu”:,
“\uuu getslice\uuuu”:,
“\uuugt\uuuuuu”:,
“\uuuu散列”:无,
"iadd":,,
“伊穆尔”:,
“\uuuu init\uuuuuuu”:,
“\uuuuu iter\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
‘乐’,
“\uuuu len\uuuuuuu”:,
“————————”,
“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“\uuu ne\uuuuu”:,
"新":,,
“报告”:,
“\uuuu反向”:,
""rmul":,,
“\uuuu setitem\uuuuuuuuu”:,
“\uuuu设置许可证”:,
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“追加”:,
“计数”:,
“扩展”:,
“索引”:,
“插入”:,
“流行音乐”:,
“删除”:,
“反向”:,
'排序':}

根据您的目标,您可以创建一个从内置类继承的类,如
list
tuple

>>> class A(tuple):
...     def speak(self):
...         print "Bark!"
... 
>>> a = A((1,2,3)) # extra parens needed to distinguish single tuple arg from 3 scalar args
>>> a * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> a.speak()
Bark!
考虑到您的向量用例,子类化tuple可能很好地解决了这个问题

import math

class Vector(tuple):
    def magnitude(self):
        return math.sqrt( self[0]*self[0]+self[1]*self[1]+self[2]*self[2] )

根据您的目标,您可以创建一个从内置类继承的类,如
list
tuple

>>> class A(tuple):
...     def speak(self):
...         print "Bark!"
... 
>>> a = A((1,2,3)) # extra parens needed to distinguish single tuple arg from 3 scalar args
>>> a * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> a.speak()
Bark!
考虑到您的向量用例,子类化tuple可能很好地解决了这个问题

import math

class Vector(tuple):
    def magnitude(self):
        return math.sqrt( self[0]*self[0]+self[1]*self[1]+self[2]*self[2] )

调用
\uuuu str\uuuu
时,该类不充当字符串,它创建并返回一个新的字符串对象。我真的不明白你在说什么……当你调用
\uuuuu str\uuuuu
时,这个类并不充当字符串,它创建并返回一个新的字符串对象。我真的不明白你在说什么……我知道关于_umul_u_uu_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u_u。然而,在我的示例中,我只是试图说明它确实返回了一个元组,而不是元组的字符串表示。它只是简单地使用一个类来充当一个类型,同时还保留了其他功能,比如调用我感兴趣的方法。也许我可以把tuple子类化