2

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

JTxx
  • 21
  • 3

2 Answers2

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.

Deepstop
  • 3,627
  • 2
  • 8
  • 21
  • I'm using tkinter in my real code and I wanted to print the sum(prices) using the `tk.Label` and I put it within the `def __init__(self, parent, controller)` of the `class secondclass()`. is there a way for it to be inside the `def __init__(self, parent, controller)` of the secondclass? – JTxx Jul 13 '19 at 02:46
1

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))
  • hi! I'm actually using tkinter in my real code, the while statement was just a replacement for the `tk.Button(self, command=my_function)` and the print was a replacement for the `tk.Label(self, text=sum(prices))`. The button is in the first class while the label was in the second class. Is there a way for the prices to be within the `__init__` of the secondclass? or if there is any other way that would produce the same result as what I expected and show it within the tkinter frame, I would also appreciate knowing how to do that. :) – JTxx Jul 13 '19 at 03:00
  • Sorry, I am not that brushed up with tkinter at the moment, but yes you can definitely call `firstclass()` and `print(sum(prices)` within the `__init__` method of the second class. After that you can just call the `secondclass()` globally and you will get the same result. – the-chaos-magus Jul 13 '19 at 03:08
  • I just tried doing that but it sent me the error TypeError: __init__() missing 2 required positional arguments: 'parent' and 'controller' i guess it's because of the `__init__` in the firstclass() in the real code has ` def __init__(self, parent, controller):` can you please suggest what I can do so that I don't need to put positional arguments? – JTxx Jul 13 '19 at 03:25
  • the same also happens when I call `secondclass()` globally, because again it has those 3 positional arguments – JTxx Jul 13 '19 at 03:30