On 2025-12-29 11:28:27 +0200, Schimon Jehudah via Python-list wrote:
> Greetings, one and all.
> 
> I am experimenting with module argparse.
> 
> I would be glad to know of your opinions about this concern.
> 
> Notice dest="class".

Your problem has nothing to do with argparse. This is a property of the
Python language itself.


> Python software
> ---------------
> 
> from argparse import ArgumentParser
> parser = ArgumentParser(description="Slixfeed OSTN news service setup.")
> 
> parser.add_argument(
>     "--class",
>     choices=["address", "hostname", "tld"],
>     dest="class")
> args = parser.parse_args()
> breakpoint()
> 
> 
> Execute the software
> --------------------
> 
> -> breakpoint()
> (Pdb) args.class
> *** SyntaxError: invalid syntax
> (Pdb) 

`class` is a keyword in Python. You can't use `class` where a variable
name or other identifier is expected.

You can access attributes with arbitrary names with getattr:

(Pdb) p getattr(args, "class")
None


But I would not recommend that. Use a non-reserved name instead.
Maybe `feed_class`. Or just `class_`, if you really don't want to be
more specific what kind of class it is.

        hjp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to