+ Reply to Thread
Results 1 to 5 of 5

Thread: return function with lists and nested lists

  1. #1

    return function with lists and nested lists

    Hi all,

    I have a question regarding the return function in a def():

    i'm aware of this typical scenario:

    def list():
    stuff = []
    stuff.append(0)
    stuff.append(1)

    return stuff

    stuff = list()
    print stuff

    -----------> will result into [0, 1, 2, 3]


    But what happens when in every list slot you want to save a vector, and a point and something else in order to return a list of stuff from a def back to the main() in order to reuse them later on on another def? is there a specific way to do that?

    because it seems that if on every slot you place different things and try to reuse them outside the def then u get a lot of these type of messages 'list is not callable', or "Could not convert Rhino.Geometry.Vector 3d object to a Point 3d" in respect of returning a vector or a point, etc

    any suggestions?

    thanks

    Pav
    Last edited by pavlos03; 10-04-2012 at 01:26 PM.

  2. #2
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    Hi,
    The following works
    python Code:
      import rhinoscriptsyntax as rs
       
      def TestReturnList():
          myList=[]
          myList.append((0,0,0)) #tuple
          myList.append(["a","b","c"]) #list
          myList.append("string")
          myList.append(rs.coerce3dpoint((1,1,1)))
          return myList
         
      def CheckReturn():
          stuff=TestReturnList()
          print stuff
          for i in range (len(stuff)):
              print stuff[i]
          print stuff[1][1]
      CheckReturn()
    (I don't know if this answers your question...)
    --Mitch
    Last edited by Mitch; 10-04-2012 at 02:18 PM.

  3. #3
    Quote Originally Posted by Mitch View Post
    Hi,
    The following works
    python Code:
      import rhinoscriptsyntax as rs
       
      def TestReturnList():
          myList=[]
          myList.append((0,0,0)) #tuple
          myList.append(["a","b","c"]) #list
          myList.append("string")
          myList.append(rs.coerce3dpoint((1,1,1)))
          return myList
         
      def CheckReturn():
          stuff=TestReturnList()
          print stuff
          for i in range (len(stuff)):
              print stuff[i]
          print stuff[1][1]
      CheckReturn()
    (I don't know if this answers your question...)
    --Mitch
    hi Mitch,

    thanks for your reply, it does clarify things up. I have though a follow-up question:

    in a long range script what's the way of not losing the grip by remembering what's in every slot? and to make the script also more readable to others and not have to go always back to the lines where the list was created.

    Is the only way to create new variables for each list again:

    tupleA = stuff[0]
    listB = stuff[1]
    stringC = stuff[2]
    etc

    inside the def you will be using it? does that sound efficient in sense of overall fewer lines and memory usage?


    any tips would be useful

    thanks

    Pav

  4. #4
    Super Moderator Mitch's Avatar
    Join Date
    May 2010
    Location
    Switzerland
    Posts
    285
    Well, if you're using lists to transfer a lot of diverse data from subroutines to the main procedure, then perhaps your subroutines could be divided up into smaller, more single purpose units. Good programming practice is to try and keep your subroutines to one main function. If you end up having lists of a bunch of different things that are difficult to remember, the code becomes less readable and understandable. In that sense my example is functional, but not very elegant or easy to understand.

    The other thing to do is to simply use comments to remind yourself and any other code readers what exactly is contained in a variable if it's not clear. It's not a bad practice to reference some part of a list with a new variable that has a name that's easier to read but if you find yourself having to do this very often, perhaps you need to look at simplifying your code and subroutines.

    --Mitch

  5. #5
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,464
    Blog Entries
    19
    As Mitch said, use comments. I would recommend using a docstring for your function definition
    http://www.python.org/dev/peps/pep-0257/
    This is a very useful convention.

    Also remember that you can return multiple items from a function which will be returned as a tuple. This can make things much easier to read
    Python Code:
      def CurveData(curve_id):
          """Returns start point, end point, and length data for a given curve. Data is returned
          as a tuple of start, end, length"""
          curve = rs.coercecurve(curve_id)
          start = curve.PointAtStart
          end = curve.PointAtEnd
          length = curve.GetLength()
          return start, end, length

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts