哪个类拥有方法和属性

发布于 2024-12-07 16:46:45 字数 353 浏览 2 评论 0原文

假设我们有这样的代码:

class C(CC):
  a = 1
  b = 2
  def __init__(self):
    self.x = None
    self.y = 1

如何在 Python 中快速找到属性或方法的定义位置?如果它属于祖先类或者它是类C的方法。您可以看到属性 a, b, x, y 。他们一定属于C类吗?或者他们可能来自祖先阶级?什么时候将类型分配给变量?

为什么不直接使用

class C(CC):
  a = 1
  b = 2
  x = None
  y = 1

谢谢

Let's say we have this code:

class C(CC):
  a = 1
  b = 2
  def __init__(self):
    self.x = None
    self.y = 1

How can I quickly find out in Python where is the attribute or method defined? If it belongs to ancestor class or if it's the method of class C. You can see attributes a, b, x, y . Must they belong to class C? or can they be from ancestor classes? When does the type is assigned to the variable?

Why not rather use

class C(CC):
  a = 1
  b = 2
  x = None
  y = 1

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必在意 2024-12-14 16:46:45

在第一个示例中,ab 是 C 类对象的属性。 (想想“静态”属性。)xy 是 C 实例的属性。 (因此,是常规实例属性。)

在第二个示例中,所有四个属性都是 C 的属性,而不是其实例的属性。

在 Python 中,您无法“声明”特定类定义的属性,这意味着一开始就没有要继承的属性定义。 (或多或少,但我不会通过引入 __slots__ 来混淆视听)。您可以通过搜索“def method_name(”来查找方法定义,并且方法定义是继承的,就像大多数 OO 语言一样。

令人困惑的是,您可以通过类的实例访问类属性,然后如果您分配为该属性添加新值,创建一个新的实例属性:

In [1]: class C(object): a=1
In [2]: c1 = C()

In [3]: c1.a
Out[3]: 1

In [5]: c1.__dict__
Out[5]: {}

In [6]: c1.a=2

In [7]: c1.__dict__
Out[7]: {'a': 2}

In [8]: c2 = C()

In [9]: c2.a
Out[9]: 1

这确实允许您通过使用类属性为实例属性提供默认值,但我不认为这是很常见的事情 - 我喜欢使用 __init__() 参数。

In the first example, a and b are attributes of the C class object. (Think "static" attributes.) And x and y are attributes of C instances. (So, regular instance attributes.)

In the second example, all four are attributes of C, not of its instances.

In Python, you can't "declare" attributes as defined by a specific class, which means there are no attribute definitions to inherit to begin with. (More or less, but I'm not going to muddle the waters by introducing __slots__). You can find method definitions by searching for "def method_name(", and method definitions are inherited as in most OO languages.

Confusingly, you can access class attributes through instances of a class, then if you assign a new value to that attribute, a new instance attribute is created:

In [1]: class C(object): a=1
In [2]: c1 = C()

In [3]: c1.a
Out[3]: 1

In [5]: c1.__dict__
Out[5]: {}

In [6]: c1.a=2

In [7]: c1.__dict__
Out[7]: {'a': 2}

In [8]: c2 = C()

In [9]: c2.a
Out[9]: 1

Which does let you give instance attribute default values by using class attributes. I don't believe this is a very common thing to do though – I favour using default values to __init__() arguments.

箹锭⒈辈孓 2024-12-14 16:46:45

为什么不使用

class C(CC):
  a = 1
  b = 2
  x = None
  y = 1

同一件事。它有四个类级属性,由类 C 的所有对象共享。

这一点,是不同的。它有两个由C 类的所有对象共享的类级属性和两个对于C 类的每个对象都是唯一的实例变量。

class C(CC):
  a = 1
  b = 2
  def __init__(self):
    self.x = None
    self.y = 1

您的两个代码示例非常不同。它们无法相提并论。

为了回答你的另一个问题,我们使用GREP和其他工具来搜索源代码。这很容易。

Why not rather use

class C(CC):
  a = 1
  b = 2
  x = None
  y = 1

This is not the same thing. This has four class-level attributes which are shared by all objects of class C.

This, is different. It has two class-level attributes shared by all objects of class C and two instance variables which are unique to each object of class C.

class C(CC):
  a = 1
  b = 2
  def __init__(self):
    self.x = None
    self.y = 1

Your two code samples are very different. They cannot be compared.

To answer your other question, we use GREP and other tools to search the source. It's easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文