Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable as statement in python?

Tags:

python

I was reading some python code and come across this. Since I mostly write C and Java (And variable as statement doesn't even compile in these language) I'm not sure what it is about in python.

What does self.current, the "variable as statement", means here? Is it just some way to print the variable out, or this is a special grammar thing / practice in dealing with exceptions in python?

class PriorityQueue():

    def __init__(self):
        self.queue = []
        self.current = 0   

    def next(self):
        if self.current >=len(self.queue):
            self.current
            raise StopIteration

        out = self.queue[self.current]
        self.current += 1

        return out
like image 969
xxbidiao Avatar asked Sep 11 '26 17:09

xxbidiao


1 Answers

It really doesn't do anything, the only way it can do anything in particular, as @Daniel said in the comments, is if self.current refers to a property method. Something like the following:

class X():
    @property
    def current(self):
        mail_admins()
        return whatever

    def next(self):
        ...

This way, calling self.current, would actually do something.

But anyways its definitely not considered good practice since a property is just that, a property, if it's supposed to do something, it should be method.

like image 121
SpiXel Avatar answered Sep 13 '26 08:09

SpiXel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!