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.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Nov 24, 2015 at 11:37
6
  • Look at your code indentation. Indentation is crucial in Python code. The last line in your code is not part of any method. Commented Nov 24, 2015 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. Commented Nov 24, 2015 at 11:40
  • the self.data.GenerateLocation(self) is in the main of the program , can you not call methods from main or something? Commented Nov 24, 2015 at 11:42
  • @Broomer what do you expect self to be, and why? Commented Nov 24, 2015 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. Commented Nov 24, 2015 at 11:45

2 Answers 2

4

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).

answered Nov 24, 2015 at 11:45
Sign up to request clarification or add additional context in comments.

1 Comment

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()
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.

answered Nov 24, 2015 at 11:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.