0

i have the following code and any trying to make a class that generates two random numbers in a list and returns them

import random

class add :

    def __init__(self):
       self.data = []

    def GenerateLocation(self):
        random1 = 10
        random2 = 20
        self.data.append(random.uniform(-random1 , random1))
        self.data.append(random.uniform(-random2 , random2))
        print(self.data)

self.data.GenerateLocation(self)

I get the error self is not defined in line 15 the self.data.GenerateLocation(self).

Can anyone explain this, I've look at the other questions on self and init but its over my head.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Broomer
  • 107
  • 10
  • Look at your code indentation. Indentation is *crucial* in Python code. The last line in your code is *not part of any method*. – Martijn Pieters Nov 24 '15 at 11:40
  • You'll have other errors when you fix that part, because there is no `GenerateLocation()` method on `self.data`, which is a list object. – Martijn Pieters Nov 24 '15 at 11:40
  • the self.data.GenerateLocation(self) is in the main of the program , can you not call methods from main or something? – Broomer Nov 24 '15 at 11:42
  • @Broomer what do you expect `self` to be, and why? – bereal Nov 24 '15 at 11:44
  • `self` is an argument to a method. The main program is not a method, so there is no `self` there. Did you mean to create an instance of the class and call the method on that? If so, you'll need to do *that*: `instance = add()`, `add.GenerateLocation()`. The `self` argument is going to be taken care of for you in that case. – Martijn Pieters Nov 24 '15 at 11:45
  • to be honest im not sure , its for an assignment and i have it done without using __init__ and self so i used the simplest method from my code to try and understand the point of self and __init__ whats the point of instance = add() when without self and init you can just say add.GenerateLocation() ? – Broomer Nov 24 '15 at 11:57

2 Answers2

3

I think you try to do this:

import random

class Add:

    def __init__(self):
       self.data = []

    def generate_location(self):
        random1 = 10
        random2 = 20
        self.data.append(random.uniform(-random1 , random1))
        self.data.append(random.uniform(-random2 , random2))
        print(self.data)

my_object = Add()
my_object.generate_location()

To use class you have to create object/instance. Creating instance python calls Add.__init__ and reserves place in memory for self.data. Then you can call its method my_object.generate_location() and it can use self.data.

self is use only inside class - it is like words me, my (my.data).

furas
  • 134,197
  • 12
  • 106
  • 148
  • yes thats exactly whats its supposed to do can you explain why the above code is correct compared having no _init__ or self and calling it by using the class name like Add.generate_location() – Broomer Nov 24 '15 at 11:49
1

The line self.data.GenerateLocation(self) is really strange and it is not clear what are you trying to do here. If you are creating the class, it seems that you want to instantinate it, i.e. create object of that class. To do so, you have to invoke constructor. In your case it will be like (copying from @furas's answer)

my_object = Add()

This creates object my_object which is instance of class Add. Then you can call method generate_location of your object. To do so, you have to prepend generate_location() with the name of your object.

my_object.generate_location()

Note that despite the fact that generate_location has argument self, it is not given explicitly in this call. This is because Python will pass (a reference to) the object my_object to the method generate_location as a first argument automatically just because it is method of this object. (Methods are functions that are associated with objects, like .append() for lists.)

See the official tutorial for more details: python 2, python 3.

Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78