ArcPy and ArcGIS(Second Edition)
上QQ阅读APP看书,第一时间看更新

Variables

Variables are a part of all programming languages. They are used to reference data objects stored in memory for using later in a script. There are a lot of arguments over the best method of naming variables. No variable standard has been developed for Python scripting for ArcGIS, so I will describe some common practices to use when naming variables here:

  1. Making them descriptive: Don't just name a variable, x; that variable will be useless later when the script is reviewed, and there is no way of knowing what it is used for, or why. They should be longer rather than shorter, and should explain what they do, or even what type of data they hold. For example:
shapefilePath = "C:/Data/shapefile.shp"
  1. Using CamelCase to make the variable readable: Camel case is a term used for variables that start with a lowercase letter but have uppercase letters in the middle, resembling a camel's hump. For example:
camelCase = 'camel case is twoWords stuck together like this'
  1. Using an underscore to separate parts of the name: This makes the name longer, but adds some clarity when reading the variable name, like this:
location_address = '100 Main St'
  1. Including the data type in the variable name: If the variable contains a string, call it variableString or variable_string. This is not standard, and will not be used in this book, but it can help organize the script, and is helpful for others who will read these scripts. Python is dynamically typed instead of statically typed, a programming language distinction, which means that a variable does not have to be declared before it can be used, unlike Visual Basic or other statically typed languages. For example:
variableString = 'this is a string'