I have class Books
and method select
in it. Also there is an instance of that class called book
. I want to be able to do both Books.select(where='...')
and book.select(where='...')
:
class Books():
def select(obj, where):
print(obj, where)
book = Books()
Books.select(where='asdf')
book.select(where='asdf')
The above obviously doesn't work, because select
is an instance bound method:
Traceback (most recent call last):
File "test.py", line 7, in <module>
Books.select(where='asdf')
TypeError: select() takes exactly 2 arguments (1 given)
A working code:
class Books():
@staticmethod
def select(obj, where):
print(obj, where)
book = Books()
Books.select(Books, where='asdf')
Books.select(book, where='asdf')
And i get:
vic@wic:~/projects/snippets$ python3 test.py
<class '__main__.Books'> asdf
<__main__.Books object at 0x17fd6d0> asdf
But i have to manually pass the class or its instance as the first argument to the select
method - not what i want.
If i make select
a class method:
class Books():
@classmethod
def select(obj, where):
print(obj, where)
book = Books()
Books.select(where='asdf')
book.select(where='asdf')
I always get a class as the first argument:
vic@wic:~/projects/snippets$ python3 test.py
<class '__main__.Books'> asdf
<class '__main__.Books'> asdf
But i want to get an instance in the second case.
So, is there a way to accomplish what i want without manually passing the class/instance as the first argument to a static method?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…