Đây là mã của tôi trông như thế nào
class InviteManager[]:
ALREADY_INVITED_MESSAGE = "You are already on our invite list"
INVITE_MESSAGE = "Thank you! we will be in touch soon"
@staticmethod
@missing_input_not_allowed
def invite[email]:
try:
db.session.add[Invite[email]]
db.session.commit[]
except IntegrityError:
return ALREADY_INVITED_MESSAGE
return INVITE_MESSAGE
Khi tôi chạy các bài kiểm tra của mình, tôi thấy
NameError: global name 'INVITE_MESSAGE' is not defined
Làm thế nào tôi có thể truy cập INVITE_MESSAGE
bên trong @staticmethod
?
Hỏi ngày 25 tháng 8 năm 2013 lúc 16:47Aug 25, 2013 at 16:47
Daydreamerdaydreamerdaydreamer
83.5K181 Huy hiệu vàng435 Huy hiệu bạc706 Huy hiệu đồng181 gold badges435 silver badges706 bronze badges
0
Bạn có thể truy cập nó dưới dạng InviteManager.INVITE_MESSAGE
, nhưng giải pháp sạch hơn là thay đổi phương thức tĩnh thành phương thức lớp:
@classmethod
@missing_input_not_allowed
def invite[cls, email]:
return cls.INVITE_MESSAGE
.
Đã trả lời ngày 25 tháng 8 năm 2013 lúc 16:51Aug 25, 2013 at 16:51
Fred Foofred FooFred Foo
348K74 Huy hiệu vàng726 Huy hiệu bạc825 Huy hiệu Đồng74 gold badges726 silver badges825 bronze badges
3
Try:
class InviteManager[]:
ALREADY_INVITED_MESSAGE = "You are already on our invite list"
INVITE_MESSAGE = "Thank you! we will be in touch soon"
@staticmethod
@missing_input_not_allowed
def invite[email]:
try:
db.session.add[Invite[email]]
db.session.commit[]
except IntegrityError:
return InviteManager.ALREADY_INVITED_MESSAGE
return InviteManager.INVITE_MESSAGE
NameError: global name 'INVITE_MESSAGE' is not defined
0 nằm trong phạm vi của nó là staticmethods.Đã trả lời ngày 25 tháng 8 năm 2013 lúc 16:51Aug 25, 2013 at 16:51
Fred Foofred Foo
348K74 Huy hiệu vàng726 Huy hiệu bạc825 Huy hiệu ĐồngNameError: global name 'INVITE_MESSAGE' is not defined
0 nằm trong phạm vi của nó là staticmethods.
Đã trả lời ngày 25 tháng 8 năm 2013 lúc 16:51Aug 25, 2013 at 16:51
Daydreamerdaydreamerdaydreamer
83.5K181 Huy hiệu vàng435 Huy hiệu bạc706 Huy hiệu đồng181 gold badges435 silver badges706 bronze badges
Bạn có thể truy cập nó dưới dạng InviteManager.INVITE_MESSAGE
, nhưng giải pháp sạch hơn là thay đổi phương thức tĩnh thành phương thức lớp:
.
return __class__.ALREADY_INVITED_MESSAGE
return __class__.INVITE_MESSAGE
Đã trả lời ngày 25 tháng 8 năm 2013 lúc 16:51
Fred Foofred FooOct 14, 2021 at 1:50
1
348K74 Huy hiệu vàng726 Huy hiệu bạc825 Huy hiệu Đồng
Đã trả lời ngày 25 tháng 8 năm 2013 lúc 16:51Aug 25, 2013 at 16:51
Fred Foofred FooMaxime Lorant
348K74 Huy hiệu vàng726 Huy hiệu bạc825 Huy hiệu Đồng19 gold badges84 silver badges96 bronze badges
0 nằm trong phạm vi của nó là staticmethods.NameError: global name 'INVITE_MESSAGE' is not defined
Chỉ nhận ra, tôi cần
NameError: global name 'INVITE_MESSAGE' is not defined
1self keyword as self keyword is used to represent instance of the class or to use instance variables of the class. In fact one use class_name, see below example:class InviteManager[]:
ALREADY_INVITED_MESSAGE = "You are already on our invite list"
INVITE_MESSAGE = "Thank you! we will be in touch soon"
@classmethod
@missing_input_not_allowed
def invite[cls, email]:
try:
db.session.add[Invite[email]]
db.session.commit[]
except IntegrityError:
return cls.ALREADY_INVITED_MESSAGE
return cls.INVITE_MESSAGE
bạn có thể đọc nó ở đâyApr 7, 2019 at 6:20