我是靠谱客的博主 过时凉面,最近开发中收集的这篇文章主要介绍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.

最后

以上就是过时凉面为你收集整理的python中属性对象不可下标是什么意思,列表元素(对象)不可下标的全部内容,希望文章能够帮你解决python中属性对象不可下标是什么意思,列表元素(对象)不可下标所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部