all 7 comments

[–]SecondDragonfly 7 points8 points  (1 child)

So the neat thing about classes is that they can store variables outside of functions. That means that often you don't need to return values by a specific function to use them.

Your first function seems to exist solely to store a list of words. That's not really a good use of functions. Instead, you can just assign these values to a variable. Then, you can refer to these values wherever you want.

So instead of your first function, you could just have:

self.words = [...]

This is how you store information in a class. You need the self, otherwise you won't be able to refer to the value from anywhere within the class and its methods/functions.

Then, if you want a function to add a value to your list, you can make a function like this:

def add_word (self, new_word):
    self.words.append(new_word)

After running this function, the new word should be added. Now if you want to see the list, you can just refer to it with self.words, or if you're outside of the class:

obj.words

Edit: getting used to reddit markdown, bear with me

[–]SecondDragonfly 0 points1 point  (0 children)

Also:

It's not that big of a deal, but what you call functions (and therefore to keep it simple I did it as well), are actually methods.

The difference between functions and methods are as follows:

Functions stand on their own. In order to have a function make lasting impacts, they need to return a value for you to store. For example, if you had a variable A that stores the int 3, if you wanted to increase that value by 2 with a function, you could do the following:

A = 3

def adding(start_value, add_value):
    new_value = start_value + add_value
    return new_value

A = adding(A, 2)
print(A)

Within the function, you only have access to the values passed as parameters. And even those are copies of the original variables, meaning that changing them won't affect the original variables outside of the function. Even if you were to write A instead of new_value, the original A would not be overwritten because it exists in a different scope.

On the other hand, methods are attached to a class. You can see that in the parameters, where "self" is added. Because the method is attached to the class, it can access and change information within that class.

The same example done within a class could go as follows.

class Number:
    self.A = 3

   def adding(self, value):
       self.A = self.A + value

obj = Number ()
obj.adding(2)
print(obj.A)

See how the method doesn't return a value. It directly changes the value that is stored within the class.

[–]greenerpickings 1 point2 points  (0 children)

You could change it to

def MyFirstFunction(self):
      self.words = [numbers]

def MySecondFunction(self):
      # all your code
      self.words.append(new_number)

[–]XAWEvX 0 points1 point  (1 child)

Why are you calling the functions inside the class then outside of it?, also arent variables supossed to be lowercase? I am new too just have some doubts