Python Forum
i want to use type= as a function/method keyword argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i want to use type= as a function/method keyword argument
#4
Correct. An argument named "type" (or "print" or "list" or ...) will hide the same-named built-in function in the function scope. Does your function need to call type()? If you want a "type" argument and you want to call the "type" function, I suggest using "type_" as the argument name. Trying to hide the argument name by popping it from **kwargs is a lot of effort and confusion just to avoid a trailing "_". It is the kind of "cute" coding trick that is not going to win you any friends from people who might have to use or maintain this code. "Why doesn't "type" show up as an argument for that_function() in autocomplete?" Static analysis will also report it as an error, if you ever do that.

If you need to be cute, I find this less egregious. Terrible, but less terrible than what you want to do.
import builtins  # <- Provides a clue that you are up to no good

def my_func(type=None):
    print(type, builtins.type(type))

my_func(5)
my_func("Hello")
Output:
5 <class 'int'> Hello <class 'str'>
Reply


Messages In This Thread
RE: i want to use type= as a function/method keyword argument - by deanhystad - Oct-31-2022, 08:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Django "Unexpected Keyword Argument 'min_value'" AstralWeeks 0 274 Mar-27-2024, 04:56 AM
Last Post: AstralWeeks
  mutable argument in function definition akbarza 1 523 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Find a specific keyword after another keyword and change the output sgtmcc 5 892 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 624 Aug-24-2023, 01:46 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,145 Dec-25-2022, 03:00 PM
Last Post: askfriends
  match type with value in csv parsing function vinci 2 1,344 Jan-21-2022, 12:19 PM
Last Post: Larz60+
Question How to pass a method as argument in an another method? anilanvesh 6 2,804 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,682 Sep-06-2021, 03:43 PM
Last Post: muzikman
  TypeError: __init__() got an unexpected keyword argument 'value' Anldra12 7 22,810 May-11-2021, 06:35 PM
Last Post: deanhystad
  How to use a tuple as an argument of a function zarox 5 3,709 Nov-14-2020, 08:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020