Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3936466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:57:04+00:00 2026-05-19T23:57:04+00:00

I have some question about functions which have default parameters. import sys from random

  • 0

I have some question about functions which have default parameters.

import sys
from random import randint

my_list = [1,2,3]
def Veli(a, b = my_list):
    my_list.append(randint(1,1500))
    print a + b[-1]

print "Number of Refs:", sys.getrefcount(my_list)
print "Def args:", Veli.func_defaults
Veli(1) # This is 4
Veli(1) # We almost always obtain different result because of randint
Veli(1) # Again different results.
print Veli.func_defaults
print "Number of Refs:", sys.getrefcount(my_list)
Veli(1)
my_list = [-5] # old my_list has different address 
print "Number of Refs:", sys.getrefcount(my_list) # Less than previous
print "Def args:", Veli.func_defaults
Veli(1) # Now gives same results.
print "Def args:", Veli.func_defaults
Veli(1) # Same result again...

Outputs: (Some numbers depend on which values randint has returned, of course.)

Number of Refs: 3
Def args: ([1, 2, 3],)
322
1119
740
([1, 2, 3, 321, 1118, 739],)
Number of Refs: 3
303
Number of Refs: 2
Def args: ([1, 2, 3, 321, 1118, 739, 302],)
303
Def args: ([1, 2, 3, 321, 1118, 739, 302],)
303

The following code gives you a number less than the previous one, because b and my_list is not referenced to same address anymore.

print "#ref:", sys.getrefcount(my_list) # Less than previous

Now, we have a way (the only way?) to reach b, default argument of the function:

Veli.func_defaults[0]

Sorry about my long explanation. Here is my questions:

  1. Is this a problem? Dictionary of my module crush my_list variable then now my_list variable has default address than previous. Then function which uses global variable named my_list in its body is changing (growing) while my default argument is not. Why cannot b see global variable named my_list when a + b[-1] expression is executed? I know, b has different address with my_list (because mutual objects (like list) are guaranteed to refer to different, unique, newly created list) now, but why were Python implemented so that b cannot see the global variables when b is a function arguments? Could you explain comprehensively?

  2. Is there any way to get same results with Veli.func_defaults[0]? This code executed then let say, I want to change my default arguments of my function named Veli. I cannot do this with my_list, because my_list and b have different addresses. One way is changing the elements of the list, Veli.func_defaults[0]. Is there any different way(s)?

  3. (It is not so related to code above) How can get addresses of variable? For example, how can get address of b? I use built-in function such as __hash__ , but there should be more appropriate way.

Notes:

a) This codes may be useless for any reason, but I want to learn opinions.
b) Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:57:05+00:00Added an answer on May 19, 2026 at 11:57 pm
    1. Defaults arguments are bounds at function definition time. The line def Veli(a, b = my_list): puts a reference to the object my_list happens to refer to at that time into func_defaults. Python never uses pass-by-reference – variables hold references and those references are always passed by value. So what’s actually saved in b, a reference (pointer), is copied and nobody remembers where it came from or bothers to update it.
    2. Not quite sure what you’re asking, please clarify. b will be Veli.func_defaults[0] if no second argument was passed, but obviously different if it is passed (well, unless of course the caller accesses func_defaults… you should assume he doesn’t).You could, as suggested in another answer, make bdefault to None and use the global my_list if b is None – this would give you a fresh, updated copy of its reference on every call. Perhaps you should write a class and keep the default value as an attribute of self and apply the usual (... = None): if ... is None: use = default idiom.
    3. The build-in function id. Actually, it’s implementation defined what this returns (doesn’t have to be the address), as long as it’s an integer that represents the object identity, i.e. distinct objects (with overlapping lifetimes) have a distinct id and the same object always gives the same id during its lifetime. The easy return value, which CPython chooses, is the address.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some question about WH_MOUSE. From what I've read, by putting the hook
I have some questions about the default values in a function parameter list Is
So, i have some question about xml Documents in Java. Can i get all
I have a question about reflection I am trying to have some kind of
Quick question about include/requre_once . I have some code that is common to a
I have a question about some functionality I'm trying to add to my jQuery
i have what i hope is a quick question about some code i am
I have a question about SqlDataReader: Is there some way to modificated the values
I have a question about laying out some swing components. Say, I have a
I have asked a similar question about command line arguments in C++ some hours

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.