-2

The initial question I had was whether or not a method could be called from a Python constructor. The answer here suggests that it can, but this surprised me some as, based on my reading, python cannot use something that hasn't been previously defined in the code. Looking at the answers in the linked thread, it seems like they are using a method in the constructor that is defined later in the text. How is this possible in python?

Thanks.

6
  • As long as you don't call a function before it is defined (parsed and compiled by Python), you can use it anywhere you like. Commented Apr 2, 2019 at 15:40
  • 1
    Without a simple code example it's hard to give a specific answer. But in general the constructor is defined before the methods, but the constructor is called after the methods have been defined. Commented Apr 2, 2019 at 15:40
  • 3
    Perhaps this is just a terminology problem. __init__() is an initializer, not a constructor. By the time __init__() is called, the object fully exists. Commented Apr 2, 2019 at 15:40
  • stackoverflow.com/questions/45025005/… Commented Apr 2, 2019 at 15:41
  • The actual object constructor is __new__ which amongst other things gives the object its methods, then __init__ is called to initialize the object. Commented Apr 2, 2019 at 15:43

1 Answer 1

0

When you would be running the code the class would be created , the function wouldn't be called

The function would only be called by object in main function by that time the definition of function would be compiled and known.

Try this Declare a class and then call a function which is not a member of class * and also not declared

And declare it later , it would throw an error

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.