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
#1
i want to use type= as a function/method keyword argument:
result = that_function(buffer1,buffer2,limit=4096,type=bytes)
can i successfully use type= as a keyword argument when it is defined like:
def that_function(buf1,buf2,**kwargs):
    ty = kwargs.pop("type",None)
    ...
    do_stuff(ty,buf1)
    do_stuff(ty,buf2)
    ...
    return buf1+buf2
?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Sure, but why not make type an optional argument? If "type" is in the signature it will show up in autocomplete and automatic documentation
def that_function(buf1, buf2,  *, type=None, **kwargs):
    ty =type
Reply
#3
the would mean the function cannot use "type()" in the expected form.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#5
so people generally dislike **kwargs in function prototypes?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
in my current function project, i have rid it of all **kwargs in the function prototypes/defs. the value of type is initially assigned to _type. now it has all the keywords literally expressed (most with a default value of None)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
A single leading underscore is the convention (PEP8) for marking an attribute as internal use only. A single trailing underscore is the convention used to avoid conflicts with a python keyword.
Reply
#8
You could use dtype as numpy does, if your intention is to indicate the type of the data handled by the buffers.
Reply
#9
(Nov-04-2022, 02:23 AM)deanhystad Wrote: A single leading underscore is the convention (PEP8) for marking an attribute as internal use only.
it's not really an attribute; it's merely a variable used within one file. there is no attribute for it to coflict with. i made it leading underscore because it was global and could be exported when importing the module. the underscore prevents that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(Nov-04-2022, 12:10 PM)Gribouillis Wrote: You could use dtype as numpy does, if your intention is to indicate the type of the data handled by the buffers.

the way it is being used is for the caller to indicate the string type the caller wants when the function yields lines of output. it is given one of three possible values; str, bytes, or bytearray. if given such a value then output is yield like a generator. when not used or assigned wt None, then the function dot not act as a generator and other keywords can be used to specify what is to be returned or done. for example stdout=(tuple,bytes) requests the return be a tuple of bytes.

i don't know how dtype varies, so i don't know the cases it can be used and exactly what it does. that is something to go lookup someday.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django "Unexpected Keyword Argument 'min_value'" AstralWeeks 0 95 Yesterday, 04:56 AM
Last Post: AstralWeeks
  mutable argument in function definition akbarza 1 424 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Find a specific keyword after another keyword and change the output sgtmcc 5 746 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 550 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,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  match type with value in csv parsing function vinci 2 1,268 Jan-21-2022, 12:19 PM
Last Post: Larz60+
Question How to pass a method as argument in an another method? anilanvesh 6 2,664 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,484 Sep-06-2021, 03:43 PM
Last Post: muzikman
  TypeError: __init__() got an unexpected keyword argument 'value' Anldra12 7 22,436 May-11-2021, 06:35 PM
Last Post: deanhystad
  How to use a tuple as an argument of a function zarox 5 3,463 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