I've been looking around for the best way of doing this but I haven't really found anything completely convincing.
I am writing a system where you have User objects and a collection that manages those Users. Each User has a name and I want to specify a function in the manager that can either take the name of the User or the User object itself.
class UserManager:
def remove_user(self,user_or_username):
#If user_or_username is a string
remote.remove(user_or_username)
#If user_or_username is a User object
remote.remove(user_or_username.name)
Is there any nifty way of doing this or is usage of isinstance the way to go?
A solution like mgilson's, but slightly different:
def remove_user(self,user_or_username):
try:
#If user_or_username is a User object
username = user_or_username.name
except AttributeError: #Oops -- didn't works. ask forgiveness ;-)
#If user_or_username is a string
username = user_or_username
remote.remove(username)
Why? Because this way, AttributeError
s in remove()
are not suppressed.
It might be irrelevant, but I prefer concentrating exception handling to those places I really inted to have them.
using isinstance
is a good approach... There is one more approach for this solution
if hasattr(user_or_username, 'name'):
# this object has <name> attribute
remote.remove(user_or_username.name)
else:
remote.remove(user_or_username)
Sometimes python people like to say "it's better to ask forgiveness than permission"...
def remove_user(self,user_or_username):
try:
#If user_or_username is a User object
remote.remove(user_or_username.name)
except AttributeError: #Oops -- didn't works. ask forgiveness ;-)
#If user_or_username is a string
remote.remove(user_or_username)
But I say it's just a matter of preference really. You could also use isinstance
just as easily if you know you're only going to be getting strings, or User
instances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With