Flags
Just like options change the default behavior of command line tools, flags are used to change aspects of RE behavior. You have already seen flags for ignoring case and changing behavior of line anchors. Flags can be applied to entire RE using the flags optional argument or to a particular portion of RE using special groups. And both of these forms can be mixed up as well. In regular expression parlance, flags are also known as modifiers.
Flags already seen will again be discussed in this chapter for completeness sake. You’ll also learn how to combine multiple flags.
re.IGNORECASE
First up, the flag to ignore case while matching alphabets. When flags argument is used, this can be specified as re.I or re.IGNORECASE constants.
re.DOTALL
Use re.S or re.DOTALL to allow the . metacharacter to match newline characters as well.
Multiple flags can be combined using the bitwise OR operator.
re.MULTILINE
As seen earlier, re.M or re.MULTILINE flag would allow the ^ and $ anchors to work line wise.
re.VERBOSE
The re.X or re.VERBOSE flag is another provision like named capture groups to help add clarity to RE definitions. This flag allows you to use literal whitespaces for aligning purposes and add comments after the # character to break down complex RE into multiple lines.
There are a few workarounds if you need to match whitespace and # characters literally. Here’s the relevant quote from documentation:
Whitespace within the pattern is ignored, except when in a character class, or when preceded by an unescaped backslash, or within tokens like *? , (?: or (?P . When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.
Inline comments
Comments can also be added using the (?#comment) special group. This is independent of the re.X flag.
Inline flags
- (?flags:pat) will apply flags only for this portion
- (?-flags:pat) will negate flags only for this portion
- (?flags-flags:pat) will apply and negate particular flags only for this portion
- (?flags) will apply flags for the whole RE definition
- can be specified only at the start of RE definition
- if anchors are needed, they should be specified after this group
In these ways, flags can be specified precisely only where it is needed. The flags are to be given as single letter lowercase version of short form constants. For example, i for re.I , s for re.S and so on, except L for re.L or re.LOCALE (discussed in the re.ASCII section). And as can be observed from the below examples, these do not act as capture groups.
Cheatsheet and Summary
This chapter showed some of the flags that can be used to change the default behavior of RE definition. And more special groupings were covered.
Exercises
a) Remove from the first occurrence of hat to the last occurrence of it for the given input strings. Match these markers case insensitively.
b) Delete from start if it is at the beginning of a line up to the next occurrence of the end at the end of a line. Match these markers case insensitively.
- This case sensitively
- nice and cool case insensitively
d) For the given input strings, match if the string begins with Th and also contains a line that starts with There .
Использование флага (переменная булевого типа) для начинающих
Например есть такая задача перебрать данные и определить есть ли там нужные нам для примера возьмем ситуацию из жизни: переберем мешок с картошкой, нужно узнать есть ли там гнилая мешок представим списком [] из 1 и 2, где 1 — означает гнилую картофелину.
Как бы мы это делали? сначала мы бы взяли одну картофелину и посмотрели на нее, и так делали бы до конца или пока не встретили гнилую.
А в конце основываясь на наблюдениях делали бы выводы.
На словах все понятно, но когда смотришь код тех кто только еще познает питона, то в коде они так и норовят делать вывод о присутствии(или о том что нету гнилых) сразу на первой же картошке.
Пример ошибочного поведения в коде:
Вот тут в блоке else и кроется ошибка, т. е. мы еще не закончили перебирать мешок, а выводы уже делаем.
Попробуем решить с флагом:
А как же можно научиться пользоваться флагом?
Мне например помог счетчик…
Пример: посчитаем гнилую картошку:
Следующий шаг на пути счетчик-флаг понимание того что нам не нужно считать, а достаточно изменить счетчик с 0 на любое число, например 1
Но наш счетчик уже не счетчик, у него 2 возможных значения (0, 1)
А это прям подходит для булевых переменных, которые и являются классическим флагом со значениями (True, False)
Вот мы сменили значения 0 на False, 1 на True
Теперь если сменить имя счетчика count на rot_flag, то получится первый вариант решения, т. е. с полноценным флагом.
Это не идеальная задача.
На этой задаче я попытался показать его работу и как его понять можно с помощью счетчика, считать перебор многим дается легче на восприятии чем понятие «индикатор» или флаг.
What is flags in Python?
I was playing with interactive shell, testing new stuff etc. I found a function has three flags.
Also a string has 1 flag
The question is why we have them or why do we need them?

2 Answers 2
These FLAGS signify different components of a code object. While the other answer points you towards the right answer, I am just adding a little fun to the mix.
You can try importing a subset of FLAGS from dis .
And then you can write a simple function and see what FLAGS show up:
Now, you can start increasing the complexity and see how that changes:
Here adding *args , **kwargs and yield added VARARGS, VARKEYWORDS, GENERATOR flags respectively. Now for something a little more:
And there you go, adding @coroutine decorator gives NESTED , async_def + yield gives ITERABLE_COROUTINE and the presence of a freevar, that is the function bar here, removes the NOFREE flag.
The following flag bits are defined for co_flags: bit 0x04 is set if the function uses the *arguments syntax to accept an arbitrary number of positional arguments; bit 0x08 is set if the function uses the **keywords syntax to accept arbitrary keyword arguments; bit 0x20 is set if the function is a generator.
Future feature declarations ( from __future__ import division ) also use bits in co_flags to indicate whether a code object was compiled with a particular feature enabled: bit 0x2000 is set if the function was compiled with future division enabled; bits 0x10 and 0x1000 were used in earlier versions of Python.
Other bits in co_flags are reserved for internal use.
In the Python source code, you can find this more extensive list of flags in code.h :
-
The Overflow Blog
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.6.8.43486
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Command line flags: Python and Tensorflow

I have recently came by many threads of confusion about flags in Tensorflow.
In order to quickly explain flags in Tensorflow, we have to first understand flags in Python. Here is a short tutorial.
For many Linux beginners, command line flags simply make no sense. They’re the weird things after ‘-’, such as
However, more experienced developers will know that this can be very useful because flags allow you to change the initial conditions of your code straight from command line.
I created an example program show how to use command line flags. My favourite library for flags is absl, but it can also very similarly be done with
Now, in the command line, we can run the standard
But this results in an error, expecting the flags to be defined. If we do not know what flags are defined, try typing
Name of the flag is how the flag is called, in this case -model, the second argument is the default value, and the third the description of the flag when help is called.
In general, flags are a super easy way to use command line to define some switches in the code.
Now onto machine learning.
Machine learning models are ugly (or maybe I am yet to see some nice models? Who knows…) and usually require a lot of parameters to be set at the beginning of the model. At the same time, it might be beneficial for these parameters to be easily adjustable. This is for example the case of number of neurons in neural nets, learning rate or even optimizer, because a lot of these will be subject to optimization.
Luckily, there is a version of flags specifically for Tensorflow.
Given that there is no documentation on the Tensorflow website regarding flags, I would assume that this is Google’s internal coding requirement.
However, flags are really useful and should become every developper’s tool to manage global vars in a model.
So the final question is what is the difference between using python flags and tensorflow flags. At first, it seems that they both do exactly the same thing, and since tensorflow code interacts with python code, there is no reason to use the Tensorflow flags.
However, they are small differences in functionality that might be important in deciding which flags to use. (I’m partial to Tensorflow flags, so here’s a list of reasons to choose them!)
- Tensorflow flags can occasionally be tensorflow-specific. For example, they enable us to set which GPU our code runs on. This is for example useful when you have multiple GPUs but you need your code to run on only some of them. Check the code below and see how it specifies the GPUs. Pretty cool.
2. Tensorflow flags can be set anywhere in the code and are visible from everywhere.
3. You can have the flags predefined in a json and just import them into your model.
4. The most obvious reason: You are writing Tensorflow code, so write proper Tensorflow!