1

sorry if the title does not make sense, I am relatively new to this. This is my code:

class MeanFlow:
    def __init__(self, V0=1):
        self.V0 = V0
    def LHS(self, t, y):
        return y[0]*self.V0


def velocity_field(w,f):
    z = 0 # dummy 
    u = f(z,w).real
    v = -1*f(z,w).imag 
    return u, v

w0 = 1
mean = MeanFlow()
dwdz = mean.LHS
print(velocity_field(w0, dwdz))

But I get the error TypeError: 'int' object has no attribute '__getitem__'

My question is how do I pass this function which is a method of my class instance into another function. If I define the function outside the class and pass it to another function this works but is not what I want. Thanks!

Edit: The typo return = y[0]*self.V0 has been corrected.

1
  • The code you've shown doesn't demonstrate the error you've posted. Instead, it fails to compile due to the line return = y[0]*self.V0. Commented Apr 2, 2014 at 10:50

2 Answers 2

3

What's generating TypeError: 'int' object has no attribute '__getitem__' is this:

y[0]

This is because at this point, y's value is 1, an integer, and y[0] is acting as if y is a list or string (__getitem__ is the method called to get items in lists). If y were a list (e.g. y = [1]), it'd work fine.

If you remove the [0], you're in business:

class MeanFlow:
    def __init__(self, V0=1):
        self.V0 = V0
    def LHS(self, t, y):
        return y*self.V0


def velocity_field(w,f):
    z = 0 # dummy 
    u = f(z,w).real
    v = -1*f(z,w).imag 
    return u, v

w0 = 1
mean = MeanFlow()
dwdz = mean.LHS
print(velocity_field(w0, dwdz))
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, the assignment to return was completely wrong of me, I didn't spot that. And thanks for the kind response :)
No problem! Mistakes like that are very hard to spot, especially when you've written the code yourself. Often my solution is to take a step back and imagine what the values of the variable should be in theory at each point along the way, then use print to find what they actually are.
As a followup I am wondering why it is not possible to then define w0 = [1] and use the method involving y[0]?
Sure - that'll work if we go back to y[0], so return y[0]*self.V0. However, if you set w0=[1] and then use return y*self.V0, you'll end up doing this: [1] * 1, which returns [1] (see this SO post), and .real is a property of numbers, not lists
@l4mpi I appreciate your input, i'm still getting the hang of programming and you are right I should have tested that the variables actually were what I meant them to be. I am not used to the error messages that python throws and so I assumed the error was due to something entirely different. Ill try to improve in the future!
2

There is an error in your code. You are passing 1 as the first argument to velocity_field which in turn passes it to LHS as the second argument (y). Lastly, you call __getitem__ on y by doing y[0], and that raises the exception.

Moreover, there is a syntax error as you assign the result to return.

Comments

Your Answer

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.