Python: How do I fix my code so that the append will add the argument to the list?

♀尐吖头ヾ 提交于 2019-12-12 19:22:28

问题


I am very new to python and I've been trying to do this code where i use a tkinter button command to run a function, it works but the append() is not executing, meaning it does not append to the list.

The list and the function containing the append is outside the class and is then classed within a class through the use of tkinter button command

I've tried putting the function inside the class, it works but the append is not adding into the list again.

This is the code I've made that is somewhat similar to real one

prices = []

f = True
class firstclass():
    def __init__(self):
        while f == True:
            my_function()
            f = False

def my_function():
    prices.append(70)


class secondclass():

    def __init__(self):
        pass

    print(sum(prices))

the sample of real code is in this link, please take this into consideration as well python: Appending a value to a list outside the class, function with append also outside the class, but function is called within a class

I expected that it would print the appended value which is 70, but it still printed 0


回答1:


A few issues you need to deal with. First assigning f=True outside the class won't change the value inside, so if you instantiated the class it would just throw an UnboundLocalError complaining that f isn't initialized. You can try this yourself by instantiating the class with

fc = firstclass()

Without instantiation, you have no hope of it giving you the value you want. It is printing zero because of the function secondclass, which has a print statement that is not contained within a method, so it prints the value sum(prices) which the class is declared. That value is from the original declared value of prices which is []. At least that is the way you have shown it in your question. I'm not sure whether you meant to indent the print statement, which would mean it is part of secondclass. However, if you didn't indent you would get the same result as you haven't instantiated firstclass.

To correct this, see below. This code will output 70 as you intended.

prices = []

class firstclass():
    def __init__(self):
        my_function()

def my_function():
    prices.append(70)

class secondclass():
    def __init__(self):
        pass

print('before instantiation', sum(prices))
fc = firstclass()
print('after instantiation', sum(prices))

fc is now an object of type firstclass and the __init__ method has called my_function to append the value 70 to prices.




回答2:


There are two reasons this is happening.

  1. You never called firstclass to actually initialize the constructor.
  2. You are trying to assign False to the variable f which does not belong to the scope of the class. If you still assign it, it's considered local. And at the moment the interpreter detects that you assigned it, the while loop does not have any local reference of f since you did not define it under the constructor. See this answer for more details.

Here is the completed code:

prices = []
class firstclass():
    f = True
    def __init__(self):
        while self.f:
            my_function()
            self.f = False
def my_function():
  prices.append(70)


class secondclass():
    def __init__(self):
        pass

firstclass()
print(sum(prices))


来源:https://stackoverflow.com/questions/57015545/python-how-do-i-fix-my-code-so-that-the-append-will-add-the-argument-to-the-lis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!