[英]how do you distinguish numpy arrays from Python's built-in objects
PEP8 has naming conventions for eg functions (lowercase), classes (CamelCase) and constants (uppercase). PEP8具有例如函数(小写),类(CamelCase)和常量(大写)的命名约定。
It seems to me that distinguishing between numpy arrays and built-ins such as lists is probably more important as the same operators such as "+" actually mean something totally different. 在我看来,区分numpy数组和内置命令如列表可能更重要,因为像“+”这样的运算符实际上意味着完全不同的东西。
Does anyone have any naming conventions to help with this? 有没有人有任何命名约定来帮助解决这个问题?
您可以为numpy数组使用前缀np_
,从而将它们与其他变量区分开来。
numpy arrays and lists should occupy similar syntactic roles in your code and as such I wouldn't try to distinguish between them by naming conventions. numpy数组和列表应该在代码中占用类似的语法角色,因此我不会尝试通过命名约定来区分它们。 Since everything in python is an object the usual naming conventions are there not to help distinguish type so much as usage. 由于python中的所有内容都是一个对象,因此通常的命名约定无助于区分类型和使用。 Data, whether represented in a list or a numpy.ndarray has the same usage. 无论是在列表中还是以numpy.ndarray表示的数据都具有相同的用法。
I agree that it's awkward that eg. 我同意,例如,这很尴尬。 + means different things for lists and arrays. +表示列表和数组的不同之处。 I implicitly deal with this by never putting anything like numerical data in a list but rather always in an array. 我通过永远不会将数字数据放在列表中而是总是放在数组中来隐式处理这个问题。 That way I know if I want to concatenate blocks of data I should be using numpy.hstack. 这样我知道我是否想要连接数据块我应该使用numpy.hstack。 That said, there are definitely cases where I want to build up a list through concatenation and turn it into a numpy array when I'm done. 也就是说,有些情况下我想通过连接建立一个列表,并在我完成后将其转换为一个numpy数组。 In those cases the code block is usually short enough that it's clear what's going on. 在这些情况下,代码块通常足够短,以便清楚发生了什么。 Some comments in the code never hurt. 代码中的一些注释从未受到伤害。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.