Python Forum
i want to use type= as a function/method keyword argument - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i want to use type= as a function/method keyword argument (/thread-38569.html)



i want to use type= as a function/method keyword argument - Skaperen - Oct-31-2022

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
?


RE: i want to use type= as a function/method keyword argument - deanhystad - Oct-31-2022

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



RE: i want to use type= as a function/method keyword argument - Skaperen - Oct-31-2022

the would mean the function cannot use "type()" in the expected form.


RE: i want to use type= as a function/method keyword argument - deanhystad - Oct-31-2022

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'>



RE: i want to use type= as a function/method keyword argument - Skaperen - Nov-01-2022

so people generally dislike **kwargs in function prototypes?


RE: i want to use type= as a function/method keyword argument - Skaperen - Nov-04-2022

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)


RE: i want to use type= as a function/method keyword argument - deanhystad - Nov-04-2022

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.


RE: i want to use type= as a function/method keyword argument - Gribouillis - Nov-04-2022

You could use dtype as numpy does, if your intention is to indicate the type of the data handled by the buffers.


RE: i want to use type= as a function/method keyword argument - Skaperen - Nov-06-2022

(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.


RE: i want to use type= as a function/method keyword argument - Skaperen - Nov-06-2022

(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.