The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 3 of 3

Thread: [Python]Pass by what?

  1. #1

    [Python]Pass by what?

    i just started learning Python, and I'm really confused on how it's arguments are passed..
    I've read that it is passed-by-reference, but strings and ints are passed-by-value because they are "immutable"?
    i've been Googling for a couple of minutes now, but i've just gotten more confused.
    could someone clear this up this to me?
    what/which are immutable or mutable types?
    what if I want to pass a string/int by reference, how would I do that?

  2. #2
    Lists and dictionaries are mutable. Strings, integers, and tuples are immutable. Variables merely reference objects in memory. Objects can be anything, strings, integers, tuples, list, dictionaries, custom classes, etc., variables merely reference them. Variables do not hold any data of any sort besides a pointer to an object somewhere.

    To pass a string "by reference" you would simply do:
    Code:
    def myFunction(variable):
       variable += "spam"
       return variable
    
    test = "Ni!"
    test = myFunction(test)
    print test
    Since strings are immutable you cannot change them, you can only reassign them, like this script does. Passing mutable variables by reference can be tricky and personally I consider it to be bad coding practice. Python should be succinct, but it should also be easy to understand and normally modifying lists and dictionaries by reference in a function is very confusing for someone else to read. And when you and other people are reusing code, readability is paramount.
    Last edited by Esherido; August 24th, 2008 at 10:24 AM.
    COLOURlovers | Member #2 of the kirupa XBox 360 Club
    "I think most people would agree that your computer breaking is a Christmas miracle." ~shane-c to Timmytots

  3. #3
    thanks.
    i knew that passing-by-reference was bad, but i've just gotten so used to it from AS and Java. i guess it's time to break that habit.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012