cannot change field alias without changing field name

1934
5
Jump to solution
02-03-2017 08:49 PM
MikeOnzay
Occasional Contributor III

I want to change the field alias in a feature class in a file gdb (and eventually in SDE). The alterfield_management syntax indicates I can do this, however, I get a syntax error when I try. 

AlterField_management(fc, 'Sign_repai',, 'Sign Repair')

It seems to not like the fact that I skipped the 3rd parameter.

All of the parameters except for the first two are supposed to optional. 

AlterField_management (in_table, field, {new_field_name}, {new_field_alias}, {field_type}, {field_length}, {field_is_nullable}, {clear_field_alias})

If I use the existing name as the "new field name" and then set a new alias name it works. 

Is the documentation incorrect? Or should I be doing something else?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The way to call it if you want to exclude the new_field_name argument is:

AlterField_management(fc, 'Sign_repai', new_field_alias='Sign Repair')

The documentation is correct, you are running into expected behavior.  In Python when passing optional arguments by position, instead of by keyword/name, all arguments to the left of the optional argument must be present.

>>> def arg_pass(required, optional1=None, optional2=None):
...     pass
...     
>>> arg_pass('some text', 'more text', 'even more text')
>>> 
>>> arg_pass('some text',, 'even more text')
Parsing error SyntaxError: invalid syntax (line 1)
>>> 
>>> arg_pass('some text', optional2='even more text')
>>> ‍‍‍‍‍‍‍‍

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

The way to call it if you want to exclude the new_field_name argument is:

AlterField_management(fc, 'Sign_repai', new_field_alias='Sign Repair')

The documentation is correct, you are running into expected behavior.  In Python when passing optional arguments by position, instead of by keyword/name, all arguments to the left of the optional argument must be present.

>>> def arg_pass(required, optional1=None, optional2=None):
...     pass
...     
>>> arg_pass('some text', 'more text', 'even more text')
>>> 
>>> arg_pass('some text',, 'even more text')
Parsing error SyntaxError: invalid syntax (line 1)
>>> 
>>> arg_pass('some text', optional2='even more text')
>>> ‍‍‍‍‍‍‍‍
DanPatterson_Retired
MVP Emeritus

As Joshua pointed out, you can't just skip parameters without providing an appropriate substitute.  Unfortunately, many/most of the tools in ArcMap don't show what the default is,  maybe '#', or None, but in any event, to make your life easier, just used the named syntax ( ie this_parameter='Some_Value')  The added advantage of using named parameters is you can specify then in any order you want, but! you can't mix named and in-named parameters once you have started down one of those two paths.

JoshuaBixby
MVP Esteemed Contributor

Dan, I agree, passing by keyword/name for optional parameters is easier and more consistent with the rest of the Python world.

 Your comment got me thinking, though, so I decided to take another look at the documentation.  Tucked away in the Default values section of Executing tools in the Python window, it states:

  • Optional parameters have default values. If you enter a quoted # (pound sign), "" (two double quotation marks), '' (two single quotation marks), or a Python None for an optional parameter, the default parameter value will be used. For keywords, the default value is the first keyword in the list. See the help for an individual tool for its default parameter values.

It appears there are four valid options, sort of like Choose Your Own Adventure for optional parameters.  Personally, this just feels like a recipe for disaster, or at least headache.  With it's handling of optional parameters, Esri is strongly violating The Zen of Python notion "there should be one-- and preferably only one --obvious way to do it."

DanPatterson_Retired
MVP Emeritus

Reminds me of the protracted discussions about what is None and the many links therein....there is only one None...everything is just an 'empty' wannabe

RebeccaStrauch__GISP
MVP Emeritus

Although not Zen, I agree with Dan that using "#" is acceptable, since it goes way back to the AML days (i.e. '80s" anyway). 

arcpy.AlterField_management(r'C:\__temp\test.gdb\newpts', "POINT_X", "#", "myX")

In commands, if I need to use the default or not change a value, I try "X" first, if that doesn't work ""  (i.e. double, empty quotes) sometimes works too.

0 Kudos