python中属性对象不可下标是什么意思,列表元素(对象)不可下标

I want to create a unit test for a function which sorts a list of objects according to some object attribute(s). Here is a code sample:

class Foo:

"""Custom data type Foo."""

def __init__(self,

a: str,

b: int,

c: int,

d: int,

e: int,

f: int):

self.a = a

self.b = b

self.c = c

self.d = d

self.e = e

self.f = f

self.g = c * d * e

self.h = c * d

This is a part of the unit test function which produces the error:

from random import randint

...

class Test(TestCase):

def test_item_sort_foo(self):

random_list = [Foo(str(randint(0, 1000)),

randint(0, 100),

randint(0, 100),

randint(0, 100),

randint(0, 100),

randint(0, 100))

for x in range(100)]

...

The error message is:

TypeError: 'Foo' object is not subscriptable.

Despite reading this question, I cannot understand why Python cares if Foo is subscriptable since random_list already is. I'm trying to generate a list of random Foo items similarly to the answer here. I am puzzled because I already have a (working) class of the kind

from dataclasses import dataclass

from typing import List

@dataclass

class Bar:

"""Bar custom data type."""

i: str

j: List[Foo]

And I can type, e.g.,

f = Foo(...)

b = Bar("baz", [])

b.j.append(f)

So here are my questions:

Do elements of subscriptable objects also have to be subscriptable? Why?

What is the best way to generate random data with the properties from above for testing?

How do I apply this principle for my case?

EDIT: This is the complete unit testing function:

def test_item_sort_foo(self):

random_list = [Foo(str(randint(0, 1000)),

randint(0, 100),

randint(0, 100),

randint(0, 100),

randint(0, 100),

randint(0, 100))

for x in range(100)]

sorted_list = item_sort_foos(random_list)

assert eq(sorted_list, sorted(random_list,

key=itemgetter(4, 7),

reverse=True))

And here is the code of the function which implements the logic:

def item_sort_foos(foos: List[Foo]) -> List[Foo]

return sorted(foos,

key=attrgetter("e", "h"),

reverse=True)

EDIT2: The full trace is:

Error

Traceback (most recent call last):

File "/opt/anaconda3/lib/python3.7/unittest/case.py", line 59, in testPartExecutor

yield

File "/opt/anaconda3/lib/python3.7/unittest/case.py", line 628, in run

testMethod()

File ".../src/project/tests/test_orderings.py", line 89, in test_item_sort_foos

reverse=True))

TypeError: 'Foo' object is not subscriptable

If you need any additional info, please request in the comments.

解决方案

random_list is a list of Foo objects. When you use a sort key of key=itemgetter(4, 7), you are trying to index the foo object itself. Its the same as

foo = Foo(...)

sort_key = foo[4], foo[7]

Likely you want to use key=attrgetter("e", "h") as in the item_sort_foos function you are testing.

But this is test code. item_sort_foos is a very small function and if all you do is put its guts in your unit test, you haven't tested much. Instead you should pass in some canned lists that you already know what the output is supposed to be. Compare those.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值