Decorators with arguments : Python tutorial 176

preview_player
Показать описание
Guys please help this channel to reach 20,000 subscribers. I'll keep uploading quality content for you.

Python is easy programming language to learn and anyone can learn it, and these tutorials are 100% free in hindi.

You can share this playlist with your brother, sisters and friends. This will surely add some values to their life.

If you follow this complete playlist of python tutorial surely you will learn everything about python programming language.

This video is all about 'decorators with arguments'.

Рекомендации по теме
Комментарии
Автор

Hi Harshit, could you also have a session on pandas, numpy please. Your teaching methods are great. Thanks a lot

anilalmeida
Автор

Today I can tell to my friends that I know something that u don't know 😊

rishisharma
Автор

Bhot achhha smjhate hai sir

I wanna a request that please guide us in use of this all things what u taught us


Love u sir

forestfaishion
Автор

from functools import wraps
def
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
if all([type(arg) == data_type for arg in args]):
return function(*args, **kwargs)
else:
return "Invalid Argument"
return wrapper
return decorator

@only_data_type_allow(str)
def string_join(*args):
string = ''
for arg in args:
string += arg
return string

@only_data_type_allow(int)
def number_add(*args):
return sum(args)



print(string_join("Shubham", "Raut"))
print(number_add(1, 2, 3, 4, 5))

Shubham-djk
Автор

For more clarification, you can also refer this video :

thedeadengineer
Автор

sir list comprehension use nhi kar sakte kya??

padhaiparcharcha
Автор

why do you need that big and complex decorators function just to pass only strings?
This is much better. Actually we don't need decorators only. I showed it using decorators because this is a part of decorators chapter
Code:

from functools import wraps
def allow_string_only(func_name):
@wraps(func_name)
def wrapper_func(*args, **kwargs):
return func_name(*args, **kwargs)
return wrapper_func

@allow_string_only
def add_str(*args):
string=''
for i in args:
if type(i)==str:
string+=i
# else:
# return "invalid input" # add or remove these two lines of else statement
return string

print(add_str('abc', 'def')) # output is abcdef removing the else statement
print(add_str('abc', 'def', 8, [1, 2])) # output is still abcdef removing the else statement
print(add_str('abc', 'def', 8, [1, 2, 3])) # output is 'invalid input' adding the else statement

bytetronics
Автор

5:11 iska matlab samjhe Dya ki harshit railway station ka pass rahta hai.

sushantbhagat
Автор

why there is an error when i use just --> def wappeed(*args)

RakibSUST
Автор

Sir, in above program if we want make it into only integer input function then it is throwing error as due to string=' ' .
What to do if we want to make a single program which can accept only that data type which is mentioned in @ decorator?

yugie
Автор

Ye mai nai kiya hy hm koi bhi data type pass kr sakty hain check it


from functools import wraps

def only_int_allow(any_function):
@wraps(any_function)
def wraper_function(*args, **kargs):
if all(type(i)==type(args[0]) for i in args):#jb sub datatypes 1 jaisi hon gi tb hi next function call ho ga
return any_function(*args, **kargs)
else:
return "Invalid Arguments"
return wraper_function



@only_int_allow
def add_all(*args):
total =0
for i in args:
total+=i
return total
print(add_all(1, 2, 3, 4, 5))


print("\n")
@only_int_allow
def add_all_strings(*args):
total =""
for i in args:
total+=i
return total
", "Irfan ", "Amin"))

print("\n")
@only_int_allow
def add_all(*args):
total =[]
for i in args:
total+=i
return total
print(add_all([1, 2], [3, 4], [5, 6]))

irfanamin
Автор

sir you are doing very well but please when you describing please describe flow control

anshu_elu
Автор

I got a error in if block
It says nameError : name 'function' is not defined how i remove it please tell me

lalitsaini
Автор

What if we want to pass multiple datatypes to the decorator? Like we want to add numbers and the number should be either int or float?

shikharagrawal
Автор

I write below code and i get "Invalid argument" output all the time :( please help.
from functools import wraps


def

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if all([type(arg) == int for arg in args]):
return func(*args, **kwargs)
return "Invalid arguments"
return wrapper
return decorator


@only_data_type_allow(str)
def add_all(*args):
string = ''
for i in args:
string += i
return string


print(add_all('anurag', ' varnwal'))

anuragvarnwal
Автор

Agar keyword argument pass nhi kra skte, to **kwargs ka use ku kr rhe ho

PankajDas-mwuq
Автор

i am getting error
File "<ipython-input-7-9c093bf84ca9>", line 5
if(all[type(arg) == data_type for arg in args]):
invalid syntax in for

priyankakurve
Автор

PLease explain the flow of below program:


def decodecorator(dataType, message1, message2):
def decorator(fun):
print(message1)
def wrapper(*args, **kwargs):
print(message2)
if all([type(arg) == dataType for arg in args]):
return fun(*args, **kwargs)
return "Invalid Input"
return wrapper
return decorator


@decodecorator(str, "Decorator for 'stringJoin'", "stringJoin started ...")
def stringJoin(*args):
st = ''
for i in args:
st += i
return st


@decodecorator(int, "Decorator for 'summation'\n", "summation started ...")
def summation(*args):
summ = 0
for arg in args:
summ += arg
return summ


print(stringJoin("I ", 'like ', "Geeks", 'for', "geeks"))
print()
print(summation(19, 2, 8, 533, 67, 981, 119))

dayanand
Автор

Beginners ki to halat kharab hogyi bhai ynha

Gamers_adda
Автор

Itna deep me mt jao bhai. Kuch smj nhi aa raha.

jitenvadhavana
visit shbcf.ru