Python内置类是否有属性,如果有,我如何找到可用于该类的属性?

huangapple go评论59阅读模式
英文:

Do built-in Python classes have attributes, and if so, how do I find those that are available to the class?

问题

这似乎是一个简单的问题,但经过多次搜索后,我似乎无法找到答案。我想知道,例如,列表是否具有属性。所谓属性是指可以通过点符号访问的值(而不是方法)。字符串有这些属性吗?

如果我将一个字符串值分配给一个变量:

test = 'test'

我尝试使用 dir(test),它返回了一个包含很多内容的长列表,其中包括诸如:

['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 ...]

(请注意,我从这个列表中删除了一些项目以缩短它。)下划线的项目是什么?其他项目似乎是方法。是否有属性?我如何识别属性?

对于list类的实例也有同样的问题。我如何查看该类的所有可用属性?

英文:

This feels like a simple question, but I can't seem to figure out the answer after much searching. I'm wondering if, for instance, lists have attributes. By attributes I mean values that are accessed by dot notation (not methods). Do strings have them?

If I assign a string value to a variable:

test = 'test'

I tried dir(test), which returned a long list that included stuff like:

['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 ...]

(Note that I cut items off this list to abridge it.) What are the items with underscores? The other items seem to be methods. Are there any attributes? How would I identify an attribute?

Same question for instances of the list class. How do I see all the available attributes of the class?

答案1

得分: 1

Both methods and attributes are considered attributes in python, dir() lists them all. You can check what they are by doing

test = 'test'
for attr in dir(test):
  print(f"{attr} : {type(getattr(test, attr))}")

Try it on this example, it will be more clear:

class TestClass:
  def __init__(self, a):
    self.a = a    
  def add_b(self, b):
    return self.a + b

test = TestClass(10)
for attr in dir(test):
  print(f"{attr} : {type(getattr(test, attr))}")
英文:

Both methods and attributes are considered attributes in python, dir() lists them all. You can check what they are by doing

test = 'test'
for attr in dir(test):
  print(f"{attr} : {type(getattr(test,attr))}")

Try it on this example, it will be more clear:

class TestClass:
  def __init__(self,a):
    self.a = a    
  def add_b(self, b):
    return self.a + b

test = TestClass(10)
for attr in dir(test):
  print(f"{attr} : {type(getattr(test,attr))}")

答案2

得分: 1

我会添加到之前的回答。下划线方法被称为"dunder"方法(双下划线)。双下划线用于防止与程序员可能使用的任何函数名称发生名称冲突。

这些方法通常由Python解释器调用。例如,__init__在对象实例化期间由Python调用。您可以重写这些方法以更改对象的功能。例如,您可以重写__add__方法以向自定义数据类型添加求和功能。

英文:

I will add to the previous answer. The underscore methods are known as dunder methods (double underscore). Double underscore is used to prevent name collision with any functions names that a programmer might use.

These methods are usually called by python interpreter. For example __init__ is called by python during object instantiation. You can override these methods to change the functionality of the object. For example you can override __add__ method to add summation functionality to custom data types.

huangapple
  • 本文由 发表于 2023年2月10日 11:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定