Odd error on model class method

0 views
Skip to first unread message

badfrog

unread,
Sep 7, 2007, 5:07:05 PM9/7/07
to TurboGears
class State(SQLObject):
ABBREVIATION_LENGTH = 2
NAME_LENGTH = name_field_length

abbreviation = StringCol(length=ABBREVIATION_LENGTH,
alternateID=True)
name = StringCol(length=NAME_LENGTH)

# class methods
def optionList():
list = []
for state in State.select(orderBy=State.q.abbreviation):
list.append((state.id, state.abbreviation))
return list

>>> State.optionList()
Traceback (most recent call last):
File "<console>", line 1, in ?
TypeError: unbound method optionList() must be called with State
instance as fir
st argument (got nothing instead)

Don't get this one. Why is python treating this like an instance
method? And yes, the indents line up in the original source.

Lloyd Kvam

unread,
Sep 7, 2007, 9:18:51 PM9/7/07
to turbo...@googlegroups.com
On Fri, 2007-09-07 at 14:07 -0700, badfrog wrote:
> class State(SQLObject):
> ABBREVIATION_LENGTH = 2
> NAME_LENGTH = name_field_length
>
> abbreviation = StringCol(length=ABBREVIATION_LENGTH,
> alternateID=True)
> name = StringCol(length=NAME_LENGTH)
>
> # class methods
> def optionList():
I think you need:
@classmethod
def optionList(class_):


> list = []
> for state in State.select(orderBy=State.q.abbreviation):

for state in
class_.select(orderBy=State.q.abbreviation):
> list.append((state.id, state.abbreviation))
> return list

Some people prefer klass to class_

>
> >>> State.optionList()
> Traceback (most recent call last):
> File "<console>", line 1, in ?
> TypeError: unbound method optionList() must be called with State
> instance as fir
> st argument (got nothing instead)
>
> Don't get this one. Why is python treating this like an instance
> method? And yes, the indents line up in the original source.
>
>
>

--
Lloyd Kvam
Venix Corp

badfrog

unread,
Sep 8, 2007, 2:53:33 PM9/8/07
to TurboGears
Ah ha. Thanks

Reply all
Reply to author
Forward
0 new messages