473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

default argument values qns

hi
i have declared a function like this:

def aFunction ( arg1 , arg2 = 0):
....
print type(arg2)

when i try to print the type of arg2, it gives me 'str' type..why is it
not integer type, since i have
declared it as 0 ??
thanks

Jun 1 '06 #1
3 1120
> i have declared a function like this:

def aFunction ( arg1 , arg2 = 0):
....
print type(arg2)

when i try to print the type of arg2, it gives me 'str'
type..why is it not integer type, since i have declared
it as 0 ??


def a(arg1, arg2=0): .... print type(arg2)
.... a("something")

<type 'int'>
Looks like it's an int to me. You must be doing something
spurious in your mysterious "...." that changes the data type.

-tkc

Jun 1 '06 #2
mi*******@hotmail.com wrote:
i have declared a function like this:

def aFunction ( arg1 , arg2 = 0):
....
print type(arg2)

when i try to print the type of arg2, it gives me 'str' type..why is it
not integer type, since i have declared it as 0 ??


because you or someone else is passing in a string as the second argument,
perhaps?
def aFunction(arg1, arg2=0): .... print type(arg2)
.... aFunction(1) <type 'int'> aFunction(1, 2.0) <type 'float'> aFunction(1, "two")

<type 'str'>

</F>

Jun 1 '06 #3
Le 01-06-2006, mi*******@hotmail.com <mi*******@hotmail.com> nous disait:
hi
i have declared a function like this:

def aFunction ( arg1 , arg2 = 0):
....
print type(arg2)

when i try to print the type of arg2, it gives me 'str' type..why is it
not integer type, since i have
declared it as 0 ??


You probably either called the function with a string as the second
argument, or assigned a string to arg2 in the ... part of the function.

On my box, I get what you would expect:
def aFunction ( arg1 , arg2 = 0): .... print type(arg2)
.... aFunction(2)

<type 'int'>
Now, remember that there are no variables in Python, only identifiers,
which are refering to values. When you print type(arg2), you are not
printing the "type of variable arg2", but the "type of the value
referenced by arg2", which is quite different, especially because it can
change during the execution of the program.

--
Alexandre Fayolle LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
Python et calcul scientifique: http://www.logilab.fr/science
Jun 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
2
by: Noah | last post by:
Is there a simple way to get a dictionary of argument names and their default values for a method or function? I came up with one solution, but I feel like Python must have a simpler way. ...
43
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.