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())
mob
  • 39
  • 5

1 Answers1

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.

lxop
  • 7,596
  • 3
  • 27
  • 42
Lament
  • 66
  • 2
  • 1
    thanks, is that just for functions within a class or just functions in general – mob Nov 07 '19 at 21:01
  • 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 – Lament Nov 07 '19 at 21:02
  • 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 Nov 07 '19 at 21:06
  • @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. – juanpa.arrivillaga Nov 07 '19 at 21:42