By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,642 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,642 IT Pros & Developers. It's quick & easy.

passing list to a function

P: 1
Hi,
I have a list [0.2, [1, 1.3], [1, 0.5, 2.1] ...] and need to pass this list as argument in a function and should be able to access elements in the called function, kindly help with code or hints to do this.
Thanks in advance
Feb 1 '12 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
Here's one way that encapsulates the list in an object:
Expand|Select|Wrap|Line Numbers
  1. >>> class A:
  2. ...     def __init__(self, *data):
  3. ...         self.data = data
  4. ...     def __getitem__(self, i):
  5. ...         return self.data[i]
  6. ...     
  7. >>> x = A(*[0.2, [1, 1.3], [1, 0.5, 2.1],])
  8. >>> x[1]
  9. [1, 1.3]
  10. >>> y = A(1,2,3,4,5)
  11. >>> y[0]
  12. 1
  13. >>> 
Feb 1 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.