3

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
iScrE4m
  • 882
  • 1
  • 12
  • 31
xxbidiao
  • 834
  • 5
  • 14
  • 27
  • 3
    Do you happen to have the rest of the class? – iScrE4m Sep 13 '16 at 18:42
  • It doesn't really do anything at all, it can probably be freely removed without any consequence. It doesn't even print anything. Hard to say without knowing the context of that member, maybe it's doing some sneaky black magic but nothing comes to mind. – Cory Kramer Sep 13 '16 at 18:42
  • 2
    It doesn't do anything by itself. Maybe `current` is a method with a property decorator, and the method has a side-effect? But that's a very odd way to write code. – Daniel Roseman Sep 13 '16 at 18:42
  • It's from a "complete a priority queue" fill-in practice, and this is the only 2 methods provided counting constructor in. I'll add the init() in. – xxbidiao Sep 13 '16 at 18:49
  • In that case, it does nothing. More on SpiXel's answer, which is really good :) – iScrE4m Sep 13 '16 at 18:53
  • Unless you throw out Occam's Razor, `self.current` is, and remains, an integer. – chepner Sep 13 '16 at 18:58
  • Side-note: contrary to what you claim, this is *totally* allowed in C++. – Konrad Rudolph Oct 06 '16 at 19:29

2 Answers2

3

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.

SpiXel
  • 4,338
  • 1
  • 29
  • 45
  • 1
    Sometimes it can actually be useful, such as forcing a lazy property to be computed now. But as in his example it has been computed right on the previous line, either the invocation has side effects, which is bad, or it doesn't, and calling it a second time will be useless. – spectras Sep 13 '16 at 18:52
  • Yes, it's a great insight. It's like getter and setter in multiple other languages. – xxbidiao Sep 13 '16 at 18:56
  • The in-place addition in the next clause makes this unlikely ([though possible!](http://stackoverflow.com/questions/11987949/how-to-implement-iadd-for-a-python-property)). – Adam Smith Sep 13 '16 at 18:56
0

I realized that this can be used for checking whether the attribute/method actually exist in the passed parameter. May be useful for input sanity check.

def test(value):
    try:
        value.testAttr
    except AttributeError:
        print "No testAttr attribute found"
xxbidiao
  • 834
  • 5
  • 14
  • 27