Decorator Causing All Functions to Return TrueCalling a function of a module by using its name (a string)How to return multiple values from a function?Using global variables in a functionHow to make a chain of function decorators?How to upgrade all python packages with pip?How do I list all files of a directory?python, confused in decorate and closureHow does the @property decorator work?python decorator for class methodsGet access to kwargs and args at the wrapper of decorator
Mistake in years of experience in resume?
My bank got bought out, am I now going to have to start filing tax returns in a different state?
Nails holding drywall
Complex numbers z=-3-4i polar form
Retract an already submitted recommendation letter (written for an undergrad student)
Is there metaphorical meaning of "aus der Haft entlassen"?
Find the identical rows in a matrix
Contradiction proof for inequality of P and NP?
Creating a chemical industry from a medieval tech level without petroleum
Multiple fireplaces in an apartment building?
Unknown code in script
All ASCII characters with a given bit count
Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?
How bug prioritization works in agile projects vs non agile
How do I reattach a shelf to the wall when it ripped out of the wall?
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
Does the damage from the Absorb Elements spell apply to your next attack, or to your first attack on your next turn?
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
Drawing a german abacus as in the books of Adam Ries
Which big number is bigger?
Philosophical question on logistic regression: why isn't the optimal threshold value trained?
"The cow" OR "a cow" OR "cows" in this context
Could moose/elk survive in the Amazon forest?
A Note on N!
Decorator Causing All Functions to Return True
Calling a function of a module by using its name (a string)How to return multiple values from a function?Using global variables in a functionHow to make a chain of function decorators?How to upgrade all python packages with pip?How do I list all files of a directory?python, confused in decorate and closureHow does the @property decorator work?python decorator for class methodsGet access to kwargs and args at the wrapper of decorator
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In an attempt to debug an online permissions system I was having problems with, I've created a couple of classes and functions designed to replicate the issue offline. Unfortunately, I'm having a problem, in which turning a function into a decorator completely changes the results of a decorated function.
My code is as follows, I've kept it as basic as possible, to illustrate my point.
Setting up User class:
class User(object):
def __init__(self, forename=None, surname=None, logged_in=True, exists=True, poop=False, admin=False):
self.forename = forename
self.surname = surname
self.logged_in = logged_in
self.exists = exists
self.poop = poop
self.admin = admin
def __repr__(self):
return f'User: self.forename self.surname.'
user1 = User('Paddy', 'McHugh', True, True, False, True)
user2 = User('Rodney', 'Donger', False, False, True, False)
user3 = User('Bob', 'Dangler', True, True, True, True)Creating functions to test against a user:
def user_just_is(user):
return user.exists
def user_is_poop(user):
return user.poop
def user_is_admin(user):
return user.adminTesting those functions against the chosen user with a regular function:
class Permissions2(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
else:
print('All of the conditions were met.')
Permissions2().requires(user_just_is, user_is_poop, user_is_admin)Testing those functions against the chosen user with a decorator function:
class Permissions(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
return
return func(*args, **kwargs)
return allower
return decorator
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
def print_stuff():
print('All of the conditions were met.')
print_stuff()
I'd expect the outcome of both the regular and decorator function to be the same. That if User.logged_in == False, then it would print: 'You're not logged in, please log in.'
. That if all the Boolean variables were True, it would print: 'All of the conditions were met.'
. That if any of the conditions were False, it would print: 'Not all of the conditions were met.'
.
The decorator function still returns the 'You're not logged in, please log in'
, but if User.logged_if == True, then the other Booleans don't matter, it always returns True to the all()
function and print 'All of the conditions were met.'
.
What is it about putting it in a decorator that means all()
seems to return True to all of the tested functions?
python
add a comment |
In an attempt to debug an online permissions system I was having problems with, I've created a couple of classes and functions designed to replicate the issue offline. Unfortunately, I'm having a problem, in which turning a function into a decorator completely changes the results of a decorated function.
My code is as follows, I've kept it as basic as possible, to illustrate my point.
Setting up User class:
class User(object):
def __init__(self, forename=None, surname=None, logged_in=True, exists=True, poop=False, admin=False):
self.forename = forename
self.surname = surname
self.logged_in = logged_in
self.exists = exists
self.poop = poop
self.admin = admin
def __repr__(self):
return f'User: self.forename self.surname.'
user1 = User('Paddy', 'McHugh', True, True, False, True)
user2 = User('Rodney', 'Donger', False, False, True, False)
user3 = User('Bob', 'Dangler', True, True, True, True)Creating functions to test against a user:
def user_just_is(user):
return user.exists
def user_is_poop(user):
return user.poop
def user_is_admin(user):
return user.adminTesting those functions against the chosen user with a regular function:
class Permissions2(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
else:
print('All of the conditions were met.')
Permissions2().requires(user_just_is, user_is_poop, user_is_admin)Testing those functions against the chosen user with a decorator function:
class Permissions(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
return
return func(*args, **kwargs)
return allower
return decorator
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
def print_stuff():
print('All of the conditions were met.')
print_stuff()
I'd expect the outcome of both the regular and decorator function to be the same. That if User.logged_in == False, then it would print: 'You're not logged in, please log in.'
. That if all the Boolean variables were True, it would print: 'All of the conditions were met.'
. That if any of the conditions were False, it would print: 'Not all of the conditions were met.'
.
The decorator function still returns the 'You're not logged in, please log in'
, but if User.logged_if == True, then the other Booleans don't matter, it always returns True to the all()
function and print 'All of the conditions were met.'
.
What is it about putting it in a decorator that means all()
seems to return True to all of the tested functions?
python
fwiw,print
returnsNone
– DeepSpace
Mar 22 at 16:27
Why do you keepreturn
ing the result ofprint
calls? That will always returnNone
– juanpa.arrivillaga
Mar 22 at 16:28
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to returnNone
, however, to prevent the decorated function from returning, no?
– patrick.mchugh4
Mar 22 at 16:32
add a comment |
In an attempt to debug an online permissions system I was having problems with, I've created a couple of classes and functions designed to replicate the issue offline. Unfortunately, I'm having a problem, in which turning a function into a decorator completely changes the results of a decorated function.
My code is as follows, I've kept it as basic as possible, to illustrate my point.
Setting up User class:
class User(object):
def __init__(self, forename=None, surname=None, logged_in=True, exists=True, poop=False, admin=False):
self.forename = forename
self.surname = surname
self.logged_in = logged_in
self.exists = exists
self.poop = poop
self.admin = admin
def __repr__(self):
return f'User: self.forename self.surname.'
user1 = User('Paddy', 'McHugh', True, True, False, True)
user2 = User('Rodney', 'Donger', False, False, True, False)
user3 = User('Bob', 'Dangler', True, True, True, True)Creating functions to test against a user:
def user_just_is(user):
return user.exists
def user_is_poop(user):
return user.poop
def user_is_admin(user):
return user.adminTesting those functions against the chosen user with a regular function:
class Permissions2(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
else:
print('All of the conditions were met.')
Permissions2().requires(user_just_is, user_is_poop, user_is_admin)Testing those functions against the chosen user with a decorator function:
class Permissions(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
return
return func(*args, **kwargs)
return allower
return decorator
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
def print_stuff():
print('All of the conditions were met.')
print_stuff()
I'd expect the outcome of both the regular and decorator function to be the same. That if User.logged_in == False, then it would print: 'You're not logged in, please log in.'
. That if all the Boolean variables were True, it would print: 'All of the conditions were met.'
. That if any of the conditions were False, it would print: 'Not all of the conditions were met.'
.
The decorator function still returns the 'You're not logged in, please log in'
, but if User.logged_if == True, then the other Booleans don't matter, it always returns True to the all()
function and print 'All of the conditions were met.'
.
What is it about putting it in a decorator that means all()
seems to return True to all of the tested functions?
python
In an attempt to debug an online permissions system I was having problems with, I've created a couple of classes and functions designed to replicate the issue offline. Unfortunately, I'm having a problem, in which turning a function into a decorator completely changes the results of a decorated function.
My code is as follows, I've kept it as basic as possible, to illustrate my point.
Setting up User class:
class User(object):
def __init__(self, forename=None, surname=None, logged_in=True, exists=True, poop=False, admin=False):
self.forename = forename
self.surname = surname
self.logged_in = logged_in
self.exists = exists
self.poop = poop
self.admin = admin
def __repr__(self):
return f'User: self.forename self.surname.'
user1 = User('Paddy', 'McHugh', True, True, False, True)
user2 = User('Rodney', 'Donger', False, False, True, False)
user3 = User('Bob', 'Dangler', True, True, True, True)Creating functions to test against a user:
def user_just_is(user):
return user.exists
def user_is_poop(user):
return user.poop
def user_is_admin(user):
return user.adminTesting those functions against the chosen user with a regular function:
class Permissions2(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
else:
print('All of the conditions were met.')
Permissions2().requires(user_just_is, user_is_poop, user_is_admin)Testing those functions against the chosen user with a decorator function:
class Permissions(object):
def __init__(self):
pass
def requires(self, *args):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
print('You're not logged in, please log in.')
return
if not all(i(user) for i in args):
print('Not all of the conditions were met.')
return
return func(*args, **kwargs)
return allower
return decorator
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
def print_stuff():
print('All of the conditions were met.')
print_stuff()
I'd expect the outcome of both the regular and decorator function to be the same. That if User.logged_in == False, then it would print: 'You're not logged in, please log in.'
. That if all the Boolean variables were True, it would print: 'All of the conditions were met.'
. That if any of the conditions were False, it would print: 'Not all of the conditions were met.'
.
The decorator function still returns the 'You're not logged in, please log in'
, but if User.logged_if == True, then the other Booleans don't matter, it always returns True to the all()
function and print 'All of the conditions were met.'
.
What is it about putting it in a decorator that means all()
seems to return True to all of the tested functions?
python
python
edited Mar 22 at 16:30
patrick.mchugh4
asked Mar 22 at 16:21
patrick.mchugh4patrick.mchugh4
155
155
fwiw,print
returnsNone
– DeepSpace
Mar 22 at 16:27
Why do you keepreturn
ing the result ofprint
calls? That will always returnNone
– juanpa.arrivillaga
Mar 22 at 16:28
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to returnNone
, however, to prevent the decorated function from returning, no?
– patrick.mchugh4
Mar 22 at 16:32
add a comment |
fwiw,print
returnsNone
– DeepSpace
Mar 22 at 16:27
Why do you keepreturn
ing the result ofprint
calls? That will always returnNone
– juanpa.arrivillaga
Mar 22 at 16:28
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to returnNone
, however, to prevent the decorated function from returning, no?
– patrick.mchugh4
Mar 22 at 16:32
fwiw,
print
returns None
– DeepSpace
Mar 22 at 16:27
fwiw,
print
returns None
– DeepSpace
Mar 22 at 16:27
Why do you keep
return
ing the result of print
calls? That will always return None
– juanpa.arrivillaga
Mar 22 at 16:28
Why do you keep
return
ing the result of print
calls? That will always return None
– juanpa.arrivillaga
Mar 22 at 16:28
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to return
None
, however, to prevent the decorated function from returning, no?– patrick.mchugh4
Mar 22 at 16:32
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to return
None
, however, to prevent the decorated function from returning, no?– patrick.mchugh4
Mar 22 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
The args
parameter for your allower
function shadows the args
parameter of requires
, so when you iterate over args
here:
if not all(i(user) for i in args):
you are not iterating through the list of functions passed in to requires
as args
anymore, but rather the args
passed to the decorated function. You should rename the parameter to avoid the naming conflict.
Moreover, you're defining Permissions.requires
as an instance method so its first parameter is self
, the object that the method is bound to, so when you call:
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
user_just_is
is passed as self
, rather than becoming part of args
. Since requires
does not actually make use of self
, it should be defined as a static method instead.
So with the above issues fixed, your Permissions
class should look like:
class Permissions(object):
@staticmethod
def requires(*conditions):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
return print('You're not logged in, please log in.')
if not all(i(user) for i in conditions):
return print('Not all of the conditions were met.')
return func(*args, **kwargs)
return allower
return decorator
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keeprequires
as a static method of thePermissions
class.
– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303870%2fdecorator-causing-all-functions-to-return-true%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The args
parameter for your allower
function shadows the args
parameter of requires
, so when you iterate over args
here:
if not all(i(user) for i in args):
you are not iterating through the list of functions passed in to requires
as args
anymore, but rather the args
passed to the decorated function. You should rename the parameter to avoid the naming conflict.
Moreover, you're defining Permissions.requires
as an instance method so its first parameter is self
, the object that the method is bound to, so when you call:
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
user_just_is
is passed as self
, rather than becoming part of args
. Since requires
does not actually make use of self
, it should be defined as a static method instead.
So with the above issues fixed, your Permissions
class should look like:
class Permissions(object):
@staticmethod
def requires(*conditions):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
return print('You're not logged in, please log in.')
if not all(i(user) for i in conditions):
return print('Not all of the conditions were met.')
return func(*args, **kwargs)
return allower
return decorator
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keeprequires
as a static method of thePermissions
class.
– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
add a comment |
The args
parameter for your allower
function shadows the args
parameter of requires
, so when you iterate over args
here:
if not all(i(user) for i in args):
you are not iterating through the list of functions passed in to requires
as args
anymore, but rather the args
passed to the decorated function. You should rename the parameter to avoid the naming conflict.
Moreover, you're defining Permissions.requires
as an instance method so its first parameter is self
, the object that the method is bound to, so when you call:
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
user_just_is
is passed as self
, rather than becoming part of args
. Since requires
does not actually make use of self
, it should be defined as a static method instead.
So with the above issues fixed, your Permissions
class should look like:
class Permissions(object):
@staticmethod
def requires(*conditions):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
return print('You're not logged in, please log in.')
if not all(i(user) for i in conditions):
return print('Not all of the conditions were met.')
return func(*args, **kwargs)
return allower
return decorator
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keeprequires
as a static method of thePermissions
class.
– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
add a comment |
The args
parameter for your allower
function shadows the args
parameter of requires
, so when you iterate over args
here:
if not all(i(user) for i in args):
you are not iterating through the list of functions passed in to requires
as args
anymore, but rather the args
passed to the decorated function. You should rename the parameter to avoid the naming conflict.
Moreover, you're defining Permissions.requires
as an instance method so its first parameter is self
, the object that the method is bound to, so when you call:
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
user_just_is
is passed as self
, rather than becoming part of args
. Since requires
does not actually make use of self
, it should be defined as a static method instead.
So with the above issues fixed, your Permissions
class should look like:
class Permissions(object):
@staticmethod
def requires(*conditions):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
return print('You're not logged in, please log in.')
if not all(i(user) for i in conditions):
return print('Not all of the conditions were met.')
return func(*args, **kwargs)
return allower
return decorator
The args
parameter for your allower
function shadows the args
parameter of requires
, so when you iterate over args
here:
if not all(i(user) for i in args):
you are not iterating through the list of functions passed in to requires
as args
anymore, but rather the args
passed to the decorated function. You should rename the parameter to avoid the naming conflict.
Moreover, you're defining Permissions.requires
as an instance method so its first parameter is self
, the object that the method is bound to, so when you call:
@Permissions.requires(user_just_is, user_is_poop, user_is_admin)
user_just_is
is passed as self
, rather than becoming part of args
. Since requires
does not actually make use of self
, it should be defined as a static method instead.
So with the above issues fixed, your Permissions
class should look like:
class Permissions(object):
@staticmethod
def requires(*conditions):
user = user2
def decorator(func):
@wraps(func)
def allower(*args, **kwargs):
if not user.logged_in:
return print('You're not logged in, please log in.')
if not all(i(user) for i in conditions):
return print('Not all of the conditions were met.')
return func(*args, **kwargs)
return allower
return decorator
answered Mar 22 at 16:31
blhsingblhsing
45.2k51747
45.2k51747
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keeprequires
as a static method of thePermissions
class.
– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
add a comment |
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keeprequires
as a static method of thePermissions
class.
– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
It should just be defined outside the class
– juanpa.arrivillaga
Mar 22 at 16:41
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keep
requires
as a static method of the Permissions
class.– blhsing
Mar 22 at 17:04
Static methods can always be defined outside the class, but are usually defined as static methods just to group similarly-purposed functions together for better readability. So if the OP does have other permission-related decorators not shown here then it can make sense to keep
requires
as a static method of the Permissions
class.– blhsing
Mar 22 at 17:04
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
The natural way to namespace a bunch of functions that don't require shared state is to just use a module. At the end, though, it is a matter of taste.
– juanpa.arrivillaga
Mar 22 at 17:18
1
1
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
@blhsing you absolute hero, that's done the trick! Thanks very much!
– patrick.mchugh4
Mar 22 at 19:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303870%2fdecorator-causing-all-functions-to-return-true%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
fwiw,
print
returnsNone
– DeepSpace
Mar 22 at 16:27
Why do you keep
return
ing the result ofprint
calls? That will always returnNone
– juanpa.arrivillaga
Mar 22 at 16:28
@DeepSpace @juanpa.arrivillaga Apologies, that was just a leftover from when I was testing if that made a difference. Edited now. I do want to return
None
, however, to prevent the decorated function from returning, no?– patrick.mchugh4
Mar 22 at 16:32