Calling method of an object vs. calling that same method of an object in a list

Preformatted text

#This works
#It calls a randomizing method of

a Super_Point class

mypoint.= Super_Point()
print(mypoint.red)
mypoint.randpoint
print(mypoint.red)

output 78

196

#This does not work

This does not call the randomizing method

SupPoints
for r in range(NUM_POINTS_IN_LINE):
SupPoints.append(Super_Point())

for obj in SupPoints:
print(“Before”, SupPoints[obj].red)
obj.randpoint
print(“After”, SupPoints[obj].red)

#Output Before 124

After 124

Nor does this

for r in range(NUM_POINTS_IN_LINE)
print("Before ", SupPoints[r].red)
SupPoints[r].randpoint
print("After ", SupPoints[rj].red)

#Output Before 124

After 124

Please use ‘preformatted text’ in the post editor to post your code, so that the formatting and punctuation won’t be lost or changed.

Thank you for responding

#This works
#It calls a randomizing method of the Super_Point class

mypoint.= Super_Point()
print(mypoint.red)
mypoint.randpoint
print(mypoint.red)

output 78

196

This does not work

SupPoints

for r in range(NUM_POINTS_IN_LINE):
SupPoints.append(Super_Point())

This does not call the randomizing method

for obj in SupPoints:
print(“Before”, SupPoints[obj].red)
obj.randpoint
print(“After”, SupPoints[obj].red)

#Output Before 35

After 35

Nor does this

for r in range(NUM_POINTS_IN_LINE)
print("Before ", SupPoints[r].red)
SupPoints[r].randpoint
print("After ", SupPoints[rj].red)

#Output Before 124

After 124

Unfortunately that still didn’t work. There is no indentation, and the initialization of SupPoints displays oddly. There is also at least one syntax error (the last for loop is missing the colon). If it’s easier, you can paste your code sample on a pastebin site or a GitHub gist, where these issues won’t occur.

Thanks again. I will explore your advice.

class Super_Point:

def __init__(self):
    self.red = int(random.random()*255)
    self.green = int(random.random()*255)
    self.blue = int(random.random()*255)
    self.tot_el = 1

def randpoint(self):
    self.red = int(random.random()*255)
    self.green = int(random.random()*255)
    self.blue = int(random.random()*255)
def main():
    NUM_PTS_IN_LINE = 100

    mypoint = Super_Point()
    print(mypoint.red)
    mypoint.randpoint()
    print(mypoint.red)
    SupPoints = []
# This Works

#This does not work
#Create list of 100 radomized super points
    for r in range(NUM_PTS_IN_LINE): 
        SupPoints.append(Super_Point())

    for g in SupPoints:
        print("before", g.red)
        g.randpoint
        print("after", g.red)


main()

Just to this bit: Michael, paste your code between triple backticks,
like this:

```
your code pasted here
```

That will tell discourse to keep the indenting. I know it’s there
because it’s in the “text/plain” version of the post email.

Cheers,
Cameron Simpson cs@cskk.id.au

You say this works:

mypoint = Super_Point()
print(mypoint.red)
mypoint.randpoint()
print(mypoint.red)
SupPoints = []

and that this does not:

for g in SupPoints:
    print("before", g.red)
    g.randpoint
    print("after", g.red)

The obvious thing here is that you do not call g.randpoint(). You just
reference the method:

g.randpoint

That’s a valid Python expression. But it doesn’t call the method
without some brackets. It just makes a reference to the method. That’s
not as pointless as you might think. g.randpoint is a callable:

# get a reference to the method
# (specificly, the method bound to the "g" object)
f = g.randpoint
... later ...
# call the method, randomises "g" now
f()

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like
class Super_Point:
    def __init__(self):
        self.red = int(random.random()*255)
        self.green = int(random.random()*255)
        self.blue = int(random.random()*255)
        self.tot_el = 1

        def randpoint(self):
        self.red = int(random.random()*255)
        self.green = int(random.random()*255)
        self.blue = int(random.random()*255)
def main():
    NUM_PTS_IN_LINE = 100

    mypoint = Super_Point()
    print(mypoint.red)
    mypoint.randpoint()
    print(mypoint.red)
    SupPoints = []
# This Works

#This does not work
#Create list of 100 radomized super points
    for r in range(NUM_PTS_IN_LINE): 
        SupPoints.append(Super_Point())

    for g in SupPoints:
        print("before", g.red)
        g.randpoint
        print("after", g.red)


main()

That did the trick! Cheers.