0

Getting a "this code will never be reached" under self.weight, for some reason. The code all works, however the self.weight inst returned when i print this function

class Person:
    def __init__(self,name,weight):
        self.name=name
        self.weight=weight

    def BMI(self):
        return self.name
        return self.weight


person1=Person("john",52)
print(person1.BMI())
2
  • Why do you have return self.name at all? What output were you expecting? Commented Nov 7, 2019 at 21:00
  • just testing classes out for the first time Commented Nov 7, 2019 at 21:02

1 Answer 1

5

Functions can only return one value and terminate upon the first encountered return statement. For that reason, calling BMI() returns the name and

return self.weight.

statement isn't reached.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, is that just for functions within a class or just functions in general
All functions can only return one value. that said, the value can be a tuple (an ordered pair/triplet/etc), so there are ways around that restriction
so if within the BMI function, I wanted to return the name john and his weight of 52, how would I do this, because return(self.name+self.weight) wont work
@mob all functions. Really there is no difference if your function is within a class or not except one thing, which is when that function is accessed as an attribute of an instance of that class, the first argument is passed the instance itself. This is why the first argument is conventionally named self, but note, it is simply a function for all other purposes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.