-5

Say in same file I have defined a class and function by the same name; then how will python interpret, if call is made to the constructor or the function.

class Main():
   def __init__():
      pass

def Main():
   pass

when I import this file and call:

obj = Main()

How would python interpret ?

DoOrDoNot
  • 1,132
  • 1
  • 10
  • 22
  • 7
    Did you try doing it? – jme Aug 14 '15 at 19:14
  • 7
    It would take you less time to open an interpreter, run exactly what you put here, then print `type(obj)` than it did to log in and ask this question. – Two-Bit Alchemist Aug 14 '15 at 19:15
  • 3
    There is no substitute for trying for yourself. – mproffitt Aug 14 '15 at 19:17
  • 1
    I knew what the output was. I wanted to know how was python interpreter deciding whether to execute the function or class constructions . – DoOrDoNot Aug 14 '15 at 20:09
  • 1
    The question is not at all lousy and reflects curiosity. But could have been structured bit differently. I think what you were interested in, is the internals of the code snippet, and definitely not the output, ' as others wrongly interpreted '. – Eric Shelhamer Aug 23 '15 at 20:12

1 Answers1

3

The class and the def commands each create an object and bind a local name to it. In the first instance, class creates a class object and binds the name Main to it. In the second instance, def creates a code object and binds the name Main to it.

The name Main is bound to two objects in turn. First, to a class object and next to a code object. This is nearly identical to what happens when a variable is assigned. Consider this code snippet:

Main = 'foo'
Main = 1 + 2

In the first line, the interpreter creates* a str object and binds the name Main to it. In the second line, the interpreter creates* an int object and binds the name Main to it.

So the case of having a class definitioon followed by a similarly-named function definition is identical to the case of multiple assignments. Specifically, the name continues to be bound to whatever object it was bound to most recently. (Either a code object or 3, in the examples above.)

If the previous object no longer has any references, then it is subject to deletion and garbage collection. Precisely how it is deleted and/or collected is an implementation detail that need not concern us.

*The assignment operations above might not create the object on the right-hand side. If 'foo' or 3 already exists, then it might be re-used. See "immutable objects" and/or "string interning" for more info.

References:

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Not sure why you were downvoted, while the question itself is lazy, your write up is a nice explanation – Parker Aug 14 '15 at 19:47
  • Name binding is, I think, one of those areas where the answer is obvious only after you know the answer. If OP understands the Python data model, then his question is surely lazy. If OP is coming from a traditional language background with declarations and assignments, then his question makes perfect sense. Remember in C (and similar languages) multiple declarations of the same name only create one object. – Robᵩ Aug 14 '15 at 19:55
  • I meant why your answer was initially at -1 :). Seems the downvoter has reversed his decision – Parker Aug 14 '15 at 19:58
  • I downvoted. I did it because I think this is a stupid question, not a bad answer. Why downvote the answer? Because answering this question benefits pretty much two people: the answerer, who gets rep and possibly the Reversal badge, and the OP. Meanwhile, now this terrible question won't be automatically deleted, and it will either remain garbage or more editors, possibly moderators, have to bother to clean it up because now it's here to stay. I got outvoted so it doesn't matter, but I downvoted not to say, "This is a bad answer," but "we at SO should not answer these questions." – Two-Bit Alchemist Aug 17 '15 at 15:49
  • Thanks, @Two-BitAlchemist, for the explanation. – Robᵩ Aug 17 '15 at 15:53
  • @Robᵩ Thanks for not taking it personally. FWIW, it's a great answer and if you do get Reversal out of it you deserve it. – Two-Bit Alchemist Aug 17 '15 at 15:55