Re: Challenging argparse with keyword dest="class"

"Peter J. Holzer" <[email protected]> Mon, 29 Dec 2025 10:54:33 +0100
Newsgroups gmane.comp.python.general
Message-ID <2b2735ttym6mcdaaftnz5fjzqhe5wdqxeh5qrjfrou6ynqjdhf@amaceipyxfan>
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!"
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmlST9AACgkQ8g5IURL+
KF2W3A//UX+wzysHWT59ax6+SoiYjdtP/1MvJzXe7d8X8GbjiIFSwlTwksKDlg3B
iAae0YP6XX14yflZltDpQsVY3Gg3mBJ7vc+G1VPwKJ+xf/g4ScZfOXeI9QHI9LhI
GVpdI1diaMwO7DOyAhXpktNCcagELIBCrLBpguuUyq/lECiIDyO9uaWixgEhpXQw
vQreV1jEql/540+QuJUmzdFDlHOMYn6bNzwvgk1u+c8OiW9NiiZxiomazTATHEZY
ua7NknKswJGJLetxEfO+d3fGU/7CTNsb+KNcYBDq7irr++R3OUhe98uFhQF+8RZK
9hjpXFwWUTkCzGIIkFtXCWmvpU+kV5nomQ1j5Rp1Uys5GhLqHi8zlo9zy6jg01kp
izrWL6M4j43LRr82PffHfeC8V45kf4y+YfvfQhf/4rCfwbIJhkYo9kkC0lJ2aPGn
MgZcifu7x82r4x8RU4dUUlaCYSQQGZQe/bJYXWw4hT7P6cIgdvAMn9qCr/Izsr+t
UYUxdFynUWAYn3unwMso6rX+WZ1tSbx0gd57KcRxEfAb89yUXcBsnPpst3igYMI4
AuNXQT+YF7NBMTNxEYtxMV6bKOYkKXet1Qa/c/HWuOKlLkFMvuALUsuiNAVcjeVa
g+1f8YBIfNYat/d3Xbc7pRRhRRMB6L9Zq+mUQ2mmg8jmIJ9YZ/0=
=/WTN
-----END PGP SIGNATURE-----