0

Some Python standard libraries use flags like this:

re.match(pattern, str, re.MULTILINE | re.IGNORECASE)

I am wondering, how to implement that, if you are creating a class yourself. I have searched the Internet and found that: Python: passing flags to functions That question is not satisfying for me, as it only show's the approach of saying

bla.function(argument, flag1=0, flag3=1)

But I really want it like

bla.function(argument, bla.SOMEFLAG | bla.SOMEOTHERFLAG)

is this possible?

Furthermore is it possible to create these flags, without letting them refer to an actual value?, so you really ask for bla.SOMEFLAG in the code and not for an int, that is represented by the flag? An example usage for that would be:

mask_list = [
    [             15,           "foo",           "bar"],
    [bla.TRANSPARENT,           "egg",            14.3],
    [        (4,7,2), {"name":"john"}, bla.TRANSPARENT]
]

where mask_list represents a mask(containing any elements), which can later be compared to another list by a function, all indexes where a bla.TRANSPARENT flag is set, get ignored by the comparison.

If someone could pass me a link to a file, where this is explained, or could come up with a simple explanation, I would be really grateful. (I am using Python3)

Thanks in advance!

Blarify
  • 29
  • 1
  • 11
  • You could just look at your Python's library source files. `re.py` for example will show you exactly how `re` implements its flags. They're just global variables in the module. – Duncan Jul 20 '17 at 15:38
  • @Duncan That's a good idea, that's exactly what I am doing right now, in this file the flag IGNORECASE results in sre_compile.SRE_FLAG_IGNORECASE, I have taken a look at sre_compile.py, SRE_FLAG_IGNORECASE is nowhere set inside there and I also can't figure out how the "|" has to be implemented by looking at this file. – Blarify Jul 20 '17 at 15:55
  • The `|` is just bitwise or: the flags are 1, 2, 4, 8, ... and you or them together to pass more than one. They're actually in `sre_constants.py` and `sre_compile.py` does a `from sre_constants import *` – Duncan Jul 20 '17 at 15:59

1 Answers1

0

There is nothing stopping you doing this:

bla.function(argument, bla.SOMEFLAG | bla.SOMEOTHERFLAG)

if your function signature is

def function(arg, flags):

But | is a bitwise operator and so it implies that its operands are integers, or integer-like. And so bla.SOMEFLAG and bla.SOMEOTHERFLAG must be integer values for this to work.

The other option is to use keyword parameters:

bla.function(argument, SOMEFLAG = True, SOMEOTHERFLAG = True)

and define your function like this:

def function(arg, **flags):

Inside that function, flags will be:

{'SOMEFLAG': True, 'SOMEOTHERFLAG': True}
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • So if I create two variables inside class bla like SOMEFLAG = 3 and SOMEOTHERFLAG = 7, and then pass bla.function(argument, bla.SOMEFLAG | bla.SOMEOTHERFLAG) it would set flags to an int with value 10? - That's nice, do you also have an approach for the second part of my question, creating such flags, without values, to use them safely as special-types in lists? – Blarify Jul 20 '17 at 16:15
  • Typically for flags you use powers of 2. If your flags are `3` and `7` then `3 | 7` will be `7`, not `10`. `|` is a bitwise `or` operator. `3 == 0b0011`, `7 == 0b0111` so if you `or` the bits you get `0b0111`. You can't have symbolic flags with no values in Python. But if you use keyword arguments they arrive in your called function as a dictionary with string keys and you can decide after the fact what you want the strings to mean. – BoarGules Jul 20 '17 at 16:21