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.

Dipole
  • 1,840
  • 2
  • 24
  • 35
  • 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`. – user2357112 Apr 02 '14 at 10:50

2 Answers2

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))
rickcnagy
  • 1,774
  • 18
  • 24
  • Sorry, the assignment to return was completely wrong of me, I didn't spot that. And thanks for the kind response :) – Dipole Apr 02 '14 at 10:52
  • 1
    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. – rickcnagy Apr 02 '14 at 10:54
  • As a followup I am wondering why it is not possible to then define w0 = [1] and use the method involving y[0]? – Dipole Apr 02 '14 at 10:57
  • 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](http://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times-in-python)), and `.real` is a property of [numbers](https://docs.python.org/2/library/numbers.html), not `lists` – rickcnagy Apr 02 '14 at 11:03
  • @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! – Dipole Apr 02 '14 at 12:51
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.

Germano
  • 2,452
  • 18
  • 25