Why is the program not printing three lines?

Rhodri James rhodri at kynesim.co.uk
Thu Mar 19 10:29:23 EDT 2020


On 19/03/2020 13:58, Souvik Dutta wrote:
> I should have been more clear
> class first():
>      print("from first")
>      def second():
>          print("from second")
> first()
> 
> When I run the above code the output is
> "from first"
> (2ND CODE)
> 
> class first():
>      print("from first")
>      def second():
>          print("from second")
> first.second()
> 
> When I run this code the output is
> "from first"
> "from second"
> 
> Thus going by the above logic
> 
> class first():
>      print("from first")
>      def second():
>          print("from second")
> first()
> first.second()
> 
> This should have given the following output
> from first
> from first
> from second

Nope.  You haven't made your logic clear, but I think you are still 
confused about when the code in the class suite is run, and still think 
of it as running when an instance is created.

Try running this code:

class first:
     print("from first")

Just that.  No instantiation, no "first()", nothing else.  Just the 
class suite itself.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list