Class with some instruction inside [duplicate]









up vote
-2
down vote

favorite













This question already has an answer here:



  • Why does a class' body get executed at definition time?

    2 answers



I have seen that if I write this code:



class Test:

print("inside class")

def __init__(self):
pass

Test()


I have this output: 'inside class'
It is strange, is a class only a function in python?
It is possible to simulate a class with a function?










share|improve this question















marked as duplicate by Håken Lid, Thierry Lathuille, eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 15:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • That code is called as the class body is defined / evaluates
    – N Chauhan
    Nov 11 at 15:50










  • Don't forget to indent your code.. as it written now, it is simply a syntax error
    – Maor Refaeli
    Nov 11 at 15:53














up vote
-2
down vote

favorite













This question already has an answer here:



  • Why does a class' body get executed at definition time?

    2 answers



I have seen that if I write this code:



class Test:

print("inside class")

def __init__(self):
pass

Test()


I have this output: 'inside class'
It is strange, is a class only a function in python?
It is possible to simulate a class with a function?










share|improve this question















marked as duplicate by Håken Lid, Thierry Lathuille, eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 15:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • That code is called as the class body is defined / evaluates
    – N Chauhan
    Nov 11 at 15:50










  • Don't forget to indent your code.. as it written now, it is simply a syntax error
    – Maor Refaeli
    Nov 11 at 15:53












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite












This question already has an answer here:



  • Why does a class' body get executed at definition time?

    2 answers



I have seen that if I write this code:



class Test:

print("inside class")

def __init__(self):
pass

Test()


I have this output: 'inside class'
It is strange, is a class only a function in python?
It is possible to simulate a class with a function?










share|improve this question
















This question already has an answer here:



  • Why does a class' body get executed at definition time?

    2 answers



I have seen that if I write this code:



class Test:

print("inside class")

def __init__(self):
pass

Test()


I have this output: 'inside class'
It is strange, is a class only a function in python?
It is possible to simulate a class with a function?





This question already has an answer here:



  • Why does a class' body get executed at definition time?

    2 answers







python class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:00

























asked Nov 11 at 15:47









asv

585719




585719




marked as duplicate by Håken Lid, Thierry Lathuille, eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 15:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Håken Lid, Thierry Lathuille, eyllanesc python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 15:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • That code is called as the class body is defined / evaluates
    – N Chauhan
    Nov 11 at 15:50










  • Don't forget to indent your code.. as it written now, it is simply a syntax error
    – Maor Refaeli
    Nov 11 at 15:53
















  • That code is called as the class body is defined / evaluates
    – N Chauhan
    Nov 11 at 15:50










  • Don't forget to indent your code.. as it written now, it is simply a syntax error
    – Maor Refaeli
    Nov 11 at 15:53















That code is called as the class body is defined / evaluates
– N Chauhan
Nov 11 at 15:50




That code is called as the class body is defined / evaluates
– N Chauhan
Nov 11 at 15:50












Don't forget to indent your code.. as it written now, it is simply a syntax error
– Maor Refaeli
Nov 11 at 15:53




Don't forget to indent your code.. as it written now, it is simply a syntax error
– Maor Refaeli
Nov 11 at 15:53












1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










No, one point in which they differ is the time when their bodies are executed.



Function and method bodies are not executed on import time, but class bodies (even nested class bodies) are.



Demo script:



class Upper:
print('Upper')
class Mid:
print('Mid')
def method(self):
class Low:
print('Low')
print('method')


Output:



$ python3
>>> import demo
Upper
Mid





share|improve this answer



























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    No, one point in which they differ is the time when their bodies are executed.



    Function and method bodies are not executed on import time, but class bodies (even nested class bodies) are.



    Demo script:



    class Upper:
    print('Upper')
    class Mid:
    print('Mid')
    def method(self):
    class Low:
    print('Low')
    print('method')


    Output:



    $ python3
    >>> import demo
    Upper
    Mid





    share|improve this answer
























      up vote
      4
      down vote



      accepted










      No, one point in which they differ is the time when their bodies are executed.



      Function and method bodies are not executed on import time, but class bodies (even nested class bodies) are.



      Demo script:



      class Upper:
      print('Upper')
      class Mid:
      print('Mid')
      def method(self):
      class Low:
      print('Low')
      print('method')


      Output:



      $ python3
      >>> import demo
      Upper
      Mid





      share|improve this answer






















        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        No, one point in which they differ is the time when their bodies are executed.



        Function and method bodies are not executed on import time, but class bodies (even nested class bodies) are.



        Demo script:



        class Upper:
        print('Upper')
        class Mid:
        print('Mid')
        def method(self):
        class Low:
        print('Low')
        print('method')


        Output:



        $ python3
        >>> import demo
        Upper
        Mid





        share|improve this answer












        No, one point in which they differ is the time when their bodies are executed.



        Function and method bodies are not executed on import time, but class bodies (even nested class bodies) are.



        Demo script:



        class Upper:
        print('Upper')
        class Mid:
        print('Mid')
        def method(self):
        class Low:
        print('Low')
        print('method')


        Output:



        $ python3
        >>> import demo
        Upper
        Mid






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 15:49









        timgeb

        47.3k116287




        47.3k116287













            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric