+ 3

python decorators

i wanna use built-in python functions as a decorator to wrap up all the functions i use in my code for example i wanna use int() function to provide a converted output to all of my functions without having to write int() many times is it possible or not.?

30th Jan 2022, 5:49 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
11 ответов
+ 6
Spammers are a part of life. Maybe they are just having a bad day. Don't let them get to you. You have an interesting idea. If you must, it is really not too hard to do. The first answer by ChaoticDawg tells you what you need to do. Here is an example: https://code.sololearn.com/cbY25zLmF7R9/?ref=app
31st Jan 2022, 4:59 AM
Bob_Li
Bob_Li - avatar
+ 9
def convert_to_int(func): def wrap(*args, **kwargs): return int(func(*args, **kwargs)) return wrap This decorator function above as; @convert_to_int Will convert the return value of any function to an int, similar to int(func(n)) It should be all that you need, depending on your code.
30th Jan 2022, 7:56 PM
ChaoticDawg
ChaoticDawg - avatar
+ 6
Decorators are functions that take a function as input. We normally don't have such builtin function.
30th Jan 2022, 9:18 PM
Oma Falk
Oma Falk - avatar
+ 5
Everything is a class. So how about you make a subclass of the main data classes that takes data types and wraps them by making a custom string output? Also, liking your own question/answer is super lame. Mabey just shouldn't do that. No reason to get mad though, like up or down votes actually do anything.
30th Jan 2022, 6:18 PM
Slick
Slick - avatar
+ 5
Slick i really appreciate your answer but not your comment im just saying that there are ppl that randomly gives minus for anything ,and im talking to the mf who gave me a minus , i wasn't talking to you bro
30th Jan 2022, 6:23 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 5
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) int is a class. By itself it can't be used as a decorator to a function as is, due to the class's implementation. You could, however, extend the class to make a decorator class. That being said, I think that might be overkill and just making a standard function decorator that does what you want will probably be simpler to implement and manage.
30th Jan 2022, 7:16 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Hi (人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) how about this . I am creating a new class called ModifiedInt which inherits all the properties of the int Class Here is my implementation >>> class ModifiedInt(int): def __init__(self,number): self.number = number >>> a = ModifiedInt(1.0) >>> a 1 >>> a = ModifiedInt(1) >>> a 1 >>> a = ModifiedInt(12.3456789) >>> a 12 I hope my answer helps you
30th Jan 2022, 7:06 PM
hamishhr
hamishhr - avatar
+ 3
hamishhr my problem here is to use built-in functions as decorators to wrap up other functions for example : @round def my_func(): a = 1 b = 2 return a/b here's the output is supposed to be a rounded number similar to round(my_func())
30th Jan 2022, 7:15 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 3
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) Like Chaotic Dawg said you could that. But that would be just an overkill
30th Jan 2022, 7:37 PM
hamishhr
hamishhr - avatar
+ 3
hamishhr yes , when i use it as decor it just takes the object not the object's call so basically you have to use your own decorator
30th Jan 2022, 7:46 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar
+ 3
ChaoticDawg exactly bro 👍👍 def intt(func): return int(func()) @intt it just needs to implement the call among other functionality
30th Jan 2022, 8:08 PM
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•)
(人◕‿◕) 𝕡𝕒𝕚𝕟𝕜𝕚𝕝𝕝𝕖𝕣 (•◡•) - avatar