如何将项目从类方法附加到另一类的列表?

发布于 2025-01-19 00:03:20 字数 544 浏览 4 评论 0原文

我想将项目附加到类A类中的列表中,但是当我在过程之后调用列表时,它只会返回一个空列表。我如何成功地附加物品?

这就是我尝试的。我在getanswer方法内实现lst.append,但附加不成功。请注意,我仅包括我的代码段,因为那里还有其他事情。我只是关心知道如何成功地申请附加功能。

class A:
  lst = []

class B:
  *#I implemented distance formula here*

class C: 
  def __init__(self, pt, tolerance):
    self.pt = pt

  def getAnswer(self, tolerance):
    self.tolerance = tolerance

    d = B(p1,p2).dist
    if d <= self.tolerance:
       lst.append(p2)

p_list = [p1, p2]
a = C(p_list, 7)

A.lst

I want to append an item to the list in Class A but when I call the list after the process, it just return an empty list. How exactly can I successfully append an item?

This is what I tried. I implement lst.append inside the getAnswer method but the appending was unsuccessful. Note that I only included snippet of my code because there are other things going on there. I am just concerned on knowing how can I successfully to the appending function.

class A:
  lst = []

class B:
  *#I implemented distance formula here*

class C: 
  def __init__(self, pt, tolerance):
    self.pt = pt

  def getAnswer(self, tolerance):
    self.tolerance = tolerance

    d = B(p1,p2).dist
    if d <= self.tolerance:
       lst.append(p2)

p_list = [p1, p2]
a = C(p_list, 7)

A.lst

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

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

发布评论

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

评论(2

草莓酥 2025-01-26 00:03:20

您的问题有很多问题,所以我已经指出了它们,然后更新了您的片段。

  • 您的c的实例被称为a,这使
  • 您对c initializer的容忍感到困惑,并且永远不会使用它,
  • lst lst 是a的类变量,但您没有使用它,而是在getanswer function
  • > getanswer 中制作了一个相同名称的新变量从未调用。
  • getanswer从不“获取”任何东西,因此函数名称有点具有误导性(修改了),
  • 您使用两个变量p1,p2在范围内不存在的b该函数,但
class A:
  lst = []

class B:
  *#I implemented distance formula here*

class C: 
  def __init__(self, pt):
    self.pt = pt
    self.tolerance = None

  def getAnswer(self, tolerance):
    self.tolerance = tolerance

    d = B(self.pt[0],self.pt[1]).dist
    if d <= self.tolerance:
       A.lst.append(p2)

p_list = [p1, p2]
c = C(p_list)
c.getAnswer(7)
A.lst

在 您的pt列表中已经存在有有效的用例,可变名称太模糊了,否则建议。

Theres lots of things wrong with your question so I've bullet pointed them and then updated your snippet.

  • your instance of C is called a which is confusing
  • You pass in tolerance to your C initializer and never use it,
  • lst is a class variable of A but you aren't using it, instead you're making a new variable of the same name in your getAnswer function
  • getAnswer is never called.
  • getAnswer never "gets" anything so the function name is a bit misleading (havent modified)
  • you initiate B with two variables p1,p2 which don't exist in the scope of the function, but already exist in your pt list
class A:
  lst = []

class B:
  *#I implemented distance formula here*

class C: 
  def __init__(self, pt):
    self.pt = pt
    self.tolerance = None

  def getAnswer(self, tolerance):
    self.tolerance = tolerance

    d = B(self.pt[0],self.pt[1]).dist
    if d <= self.tolerance:
       A.lst.append(p2)

p_list = [p1, p2]
c = C(p_list)
c.getAnswer(7)
A.lst

Whilst the above will update your list, its still odd to have C change A's list, I've left it as is because there are valid use cases and the variable names are too vague too suggest otherwise.

终难愈 2025-01-26 00:03:20

它不起作用,因为 lst 是类/函数内的局部变量。我会这样做:

class A:
    global lst
    lst = []
class C:
    #init
    def getAnswer(self, tolerance):
        #stuff
        #if
            lst.append(p2)

这是一个测试:

>>> class A:
    global lst
    lst = []

>>> class C:
    def __init__(self):
        lst.append("This is appended to the list!")

>>> C()
<__main__.C object at 0x0000021D25CF5640>
>>> """I just called the C __init__ function, let's see if it worked"""
"I just called the C __init__ function, let's see if it worked"
>>> print(lst)
['This is appended to the list!']
>>> """Yay!"""
'Yay!'
>>> 

It does not work, because lst is a local variable inside the class/function. I would do:

class A:
    global lst
    lst = []
class C:
    #init
    def getAnswer(self, tolerance):
        #stuff
        #if
            lst.append(p2)

Here is a test:

>>> class A:
    global lst
    lst = []

>>> class C:
    def __init__(self):
        lst.append("This is appended to the list!")

>>> C()
<__main__.C object at 0x0000021D25CF5640>
>>> """I just called the C __init__ function, let's see if it worked"""
"I just called the C __init__ function, let's see if it worked"
>>> print(lst)
['This is appended to the list!']
>>> """Yay!"""
'Yay!'
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文