3

I wrote a class in which has a function taking several inputs by *args, however the running result proves that it only takes arguments from the second one, as if self takes the first one, here's a simplified code:

class incorrect():
    def itera(self, *args):
        for i in args:
            print(i)

a = incorrect
a.itera(12, 23, 34)

And the output, 12 is lost:

23
34

What is the problem? How can I fix that?

Amarth Gûl
  • 1,040
  • 2
  • 14
  • 33
  • How do these answers have so many upvotes on a question that is a blatant off topic typo? – cs95 Sep 11 '17 at 12:25
  • 1
    Because noone found a duplicate before the answers were posted?! :) – MSeifert Sep 11 '17 at 12:26
  • @MSeifert It's not a duplicate... it's just a typo – cs95 Sep 11 '17 at 12:27
  • @cᴏʟᴅsᴘᴇᴇᴅ Well that https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self seems like a valid duplicate target... – MSeifert Sep 11 '17 at 12:28
  • @cᴏʟᴅsᴘᴇᴇᴅ I wasn't sure what to search for a question like this, so I decided to answer. +5 is a little ridiculous though for such a simple question. – Carcigenicate Sep 11 '17 at 12:29
  • 1
    @MSeifert Err, I've already VTC as off topic, so please do the honours. – cs95 Sep 11 '17 at 12:29
  • @Carcigenicate I wish some of my half decent answers would get 5 upvotes every now and then ;-( – cs95 Sep 11 '17 at 12:30
  • @cᴏʟᴅsᴘᴇᴇᴅ ikr. Theres been answers that I work on for half an hour that just get an accept; if that. I think the +5 so came in within like a minute. People are weird. – Carcigenicate Sep 11 '17 at 12:31
  • And this isn't really a typo is it? It could be due to a misunderstanding of syntax. – Carcigenicate Sep 11 '17 at 12:32
  • @Carcigenicate Anyone with an unsung hero badge can appreciate what you said. People can be [really](https://stackoverflow.com/questions/45164645/how-to-separate-an-integer-into-two-addends/45164689#45164689) ... [weird](https://stackoverflow.com/a/46150875/4909087) sometimes, because of how subjective a "good answer" is. – cs95 Sep 11 '17 at 12:35
  • @Carcigenicate Yes, not a typo indeed, I didn't fully understand the difference between `a = someClass` and `a = smoeClass()` until that question... – Amarth Gûl Sep 11 '17 at 12:36
  • @AmarthGûl OK good. I would review creating instances if I were you though, even if these answers helped. It's quite an important thing to have a good grasp of. – Carcigenicate Sep 11 '17 at 12:38
  • @cᴏʟᴅsᴘᴇᴇᴅ The more I use this site, the more I appreciate timing's role. It's sad, but sometimes timing Trump's quality. – Carcigenicate Sep 11 '17 at 12:39
  • @Carcigenicate Can we take a moment to also appreciate the fact that one of the most pedestrian answers I've written is also one of my most highly voted ones? x( – cs95 Sep 11 '17 at 12:41
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ Ya, I wasn't going to comment, but getting a 91 for showing the use of ceiling and round is pretty... generous. Oh well, we all get those sometimes. – Carcigenicate Sep 11 '17 at 12:45

2 Answers2

6

That's because you use it on the class and so self is 12 and args is [33, 34]. You can fix it by creating an instance:

a = incorrect()
a.itera(12, 23, 34)

Or you could omit self in the signature and use decorator @staticmethod:

@staticmethod
def itera(*args):
    for i in args:
        print(i)

Or you could make it classmethod:

@classmethod
def itera(cls, *args):
    for i in args:
        print(i)
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • I haven't downvoted but you could `print(self)` inside the function to "prove" your argument. Maybe also elaborate on the *why* `self=12` if you call it on the class :) – MSeifert Sep 11 '17 at 12:23
  • I guess it's not the *best* explication you could have provided. But it's the right one anyway. – Right leg Sep 11 '17 at 12:23
  • Instead of omitting `self` (don't do that, you will have a similar problem if you call it on an instance) make it a `@staticmethod`. – MSeifert Sep 11 '17 at 12:25
  • I was just referring to your "Or you could omit self in the signature". That's bad advise. If it shouldn't have `self` it should be a staticmethod, if it should have `cls` it should be a classmethod. – MSeifert Sep 11 '17 at 12:27
5

You're calling the method on the class itself, not an instance of the class. Create an instance, then call the method on it:

a = incorrect() # Add () to create instance
a.itera(12, 23, 34)

When you call it on the class itself, it acts as a static method, and self is no longer implicitly passed since there is no self to pass.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117