简体   繁体   English

Python:在赋值之前引用

[英]Python: Referenced before assignment

I receive the following error local variable 'ticket_reservation_expired' referenced before assignment .我收到以下错误local variable 'ticket_reservation_expired' referenced before assignment Anyone knows how to resolve this?有谁知道如何解决这个问题?

I think a solution could be to assign ticket_reservation_expired = "" before calling the function, but I wonder if that is the best way to resolve this?我认为解决方案可能是在调用函数之前分配ticket_reservation_expired = "" ,但我想知道这是否是解决此问题的最佳方法?

helpers.py helpers.py

def ticket_reservation_expired(request, timestamp_of_reservation):
    """
    * Calculate the latest possible time where tickets are still reserved.
    * Check that 'created' timestamp of reservation item is not expired.
    * If expired, error message is being generated and redirect occurs.
    """
    latest_time_before_expired = timezone.now() - timedelta(
        minutes=settings.MINUTES_TICKET_RESERVATION
    )
    if timestamp_of_reservation < latest_time_before_expired:
        messages.add_message(
            request,
            messages.ERROR,
            _("Ticket reservation is too far in the past.")
        )
        return True

views.py视图.py

ticket_reservation_expired = ticket_reservation_expired(
    self.timestamp_of_reservation
)
if ticket_reservation_expired:
    return redirect(
        'events:detail',
        organizer=self.organizer,
        event=self.event,
    )

Your function only returns a value when a certain condition is met.您的函数仅在满足特定条件时返回一个值。 When it is not met, nothing is returned.当它不满足时,什么都不返回。 Good style is to either never return a value or always return one.好的风格是要么从不返回值,要么总是返回一个值。 Since if the condition is met you return True, return False when it is not met and your error should go away.因为如果满足条件你返回 True,当它不满足时返回 False 并且你的错误应该消失。

Also, in the code you posted, you named your variable and your function the same, this will lead you to no end of trouble.此外,在您发布的代码中,您将变量和函数命名为相同的名称,这会给您带来无穷无尽的麻烦。

The problem is if the function does not return anything then it will throw an error as the variable is never defined because initialization of variable depends on the return value of the function.问题是如果函数没有返回任何东西,那么它会抛出一个错误,因为变量从未定义过,因为变量的初始化取决于函数的返回值。 Just initialize the variable with None只需用None初始化变量

ticket_reservation_expired_var = None
ticket_reservation_expired_var = ticket_reservation_expired(
    self.timestamp_of_reservation
)
if ticket_reservation_expired_var:
    return redirect(
        'events:detail',
        organizer=self.organizer,
        event=self.event,
    )

What you can do is make that name a global reference inside the function:您可以做的是将该名称作为函数内部的全局引用:

global ticket_reservation_expired

That goes on the first line of the function.这是函数的第一行。 Alternatively, add another parameter to the function ticket_reservation_expired and just pass it in when you call the function so that the value is effectively 'transferred' to the function scope.或者,向函数ticket_reservation_expired添加另一个参数,并在调用函数时将其传入,以便将值有效地“转移”到函数作用域。 However, changes to the variable will only take effect in the function's scope.但是,对变量的更改只会在函数的作用域内生效。 So global is the option if you need to make any changes 'save' themselves after the function returns.因此,如果您需要在函数返回后进行任何更改“保存”自己,则可以选择global

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM