Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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:
    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
    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


Copyright 1999 - 2012