passing list to a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohd rafiq
    New Member
    • Feb 2012
    • 1

    passing list to a function

    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's one way that encapsulates the list in an object:
    Code:
    >>> class A:
    ... 	def __init__(self, *data):
    ... 		self.data = data
    ... 	def __getitem__(self, i):
    ... 		return self.data[i]
    ... 	
    >>> x = A(*[0.2, [1, 1.3], [1, 0.5, 2.1],])
    >>> x[1]
    [1, 1.3]
    >>> y = A(1,2,3,4,5)
    >>> y[0]
    1
    >>>

    Comment

    Working...