0

I've decided to do some coding exercises at coderbyte.com and right in the first exercise (reverse a string) I found this:

def FirstReverse(str): 

Even though it works, I don't think it's a good idea to use a built-in type name as a parameter. What do you think? I know it's kinda silly, but it's my first question at stackoverflow! Thanks =)

JBC
  • 225
  • 1
  • 7

4 Answers4

1

In that function definition, str is supposed to be a string, not the built-in function str(). When you do this, you are overwriting the built-in function, so you won't be able to call str() inside the function f, but you can still using it outside. This is because scoping, you are overwriting it in the local scope of f:

def f(str):
    print str(123) # will raise an error, because 'str' is not callable (i.e. a function) anymore

f("some string here")

print str(123) # OK

This is a very common mistake, the sooner you learn to avoid it, the sooner you will become a good programmer.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Voted you up. Answer seems pretty clear to me. – Michelle Welcks Feb 06 '14 at 04:09
  • The statement *you won't be able to call str() as a function again* is straight out wrong. You only won't be able to call `str` from inside of `f`. It is a local variable. Not that the use of `str` is endorsed -- only that your answer is wrong -- I down voted it. I will reverse if corrected. :-) – dawg Feb 06 '14 at 04:22
  • @dawg Edited. But what did you mean by *"use of `str` is endorsed"*? I'm not a English native speaker and I couldn't find an appropriate translation of "endorsed". – Christian Tapia Feb 06 '14 at 04:28
  • I said "**Not** *that the use of str is endorsed*". As in -- don't use `str` as a parameter name in a function... Downvote reversed... – dawg Feb 06 '14 at 04:37
0

You can use "anything" except keywords as variable/parameter names.

It's usually not a good idea though - as in practically never.


Perhaps the example is from Python1 series where there was a string module, but no str type.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

I'm not the one that voted Christian's answer down but here's an elaboration if it helps:

Working Function

def f(string):
    print ("In function.")
    print (string)
    print (type(string))
    real_string = str(string)
    print (type(real_string))

print ("In main.")
int_val = 123
print (type(int_val))
f(int_val)

Results

In main.
In function.
123
class 'int' class 'str'

Broken Function

def f(str):
    print ("In function.")
    print (str)
    print (type(str))
    real_string = str(str)
    print (type(real_string))

print ("In main.")
int_val = 123
print (type(int_val))
f(int_val)

Results

----> 5 real_string = str(str)
TypeError: 'int' object is not callable

Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
0

The use of list, dict, str as variable names is a common newby 'mistake'.

I think that it is not a good practice for tutorials, but works OK in practice in this example. I would frown if code was presented to me with that parameters name however.

The more interesting thing to explore is WHY doesn't this tramp on the built-in str function.

Look:

>>> str(123)        # the built-in prior to the function 'f' declared
'123'
>>> def f(str):     # Note 'str' as parameter
...    return str[::-1]
... 
>>> f('123')        # function works...
'321'
>>> f(str(123))     # function 'f' works with the function 'str' prior
'321'
>>> str(123)        # built-in 'str' still works...
'123'

The answer is that in f the str parameter overrides the built-in str function only locally -- inside of f. It is 'restored' to the built-in str after the scope of f is done.

Notice:

>>> def f(str):
...    return str('123')[::-1]
... 
>>> f(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
TypeError: 'int' object is not callable

Since you are calling int inside f you are referring to the parameter, not the function. It gets confusing and, hence, not a great idea to use the name of a built-in as a variable, parameter, etc whether local or global.

dawg
  • 98,345
  • 23
  • 131
  • 206