Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChen Wang
7,371 Pointsget() and select() in peewee
to_user = models.User.get(models.User.username**username)
user = models.User.select().where(models.User.username**username).get()
In the above two sentences, what are the differences between get() and select()?
In addition, I know that "**" here means case-insensitive equal judgement. Is this usage from Flask? Obviously, this is not a python operator.
1 Answer
Dan Johnson
40,533 Pointsget will only ever return one result, and can thrown an exception if nothing is found. select returns a SelectQuery
object instead of a Model
. You could use the query to iterate through multiple elements.
I tested out those statements and they returned equivalent objects so to my knowledge they're just two different ways of doing the same thing. Here are the doc links if you want to know more:
The ** actually is a standard Python operator. With numbers it acts as the pow function. You can overload it like this:
class MyClass(object):
def __init__(self, base):
self.base = base
def __pow__(self, exponent):
return "{}^{}".format(self.base, exponent)
reals = MyClass("R")
print(reals ** 2)