0

i have a class called animals. inside of the animals class there is a function called printAnimalsNames and inside of printAnimalsNames there is another function called printAnimalsOwner how to call the printAnimalsOwner from printAnimalsNames?

for example:

class animals:
   def printAnimalsNames:
      print("Poo")
      def printAnimalsOwner:
        print("Poo : Jasmin")

how to call printAnimalsOwner from printAnimalsNames ?

1
  • 2
    You just call it normally after the definition. Not sure if this was intended but def printAnimalsNames is invalid. Commented Feb 3, 2021 at 15:43

1 Answer 1

2

You can call just like any other method as:

class Animals:
    def printAnimalsNames(self):
        print("Poo")
        x = 10

        def printAnimalsOwner():
            print("Poo : Jasmin")
            print(x)
    
        printAnimalsOwner()
        
animal = Animals()
animal.printAnimalsNames()

However, a note that you can't call it from outside of printAnimalsNames since it is local to that method only.

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

1 Comment

no. i want to call it from printAnimalsNames not outside of printAnimalsNames

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.