Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45671 > unrolled thread

@staticmethods called more than once

Started byChristian <mining.facts@gmail.com>
First post2013-05-21 08:26 -0700
Last post2013-05-21 18:23 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  @staticmethods called more than once Christian <mining.facts@gmail.com> - 2013-05-21 08:26 -0700
    Re: @staticmethods called more than once Skip Montanaro <skip@pobox.com> - 2013-05-21 10:39 -0500
    Re: @staticmethods called more than once John Gordon <gordon@panix.com> - 2013-05-21 16:34 +0000
      Re: @staticmethods called more than once John Gordon <gordon@panix.com> - 2013-05-21 16:48 +0000
        Re: @staticmethods called more than once Christian <mining.facts@gmail.com> - 2013-05-21 10:17 -0700
    Re: @staticmethods called more than once Ethan Furman <ethan@stoneleaf.us> - 2013-05-21 09:30 -0700
      Re: @staticmethods called more than once 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-21 18:23 -0700

#45671 — @staticmethods called more than once

FromChristian <mining.facts@gmail.com>
Date2013-05-21 08:26 -0700
Subject@staticmethods called more than once
Message-ID<02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com>
Hi,

i'm somewhat confused working with @staticmethods. My logger and configuration  methods are called n times, but I have only one call.  
n is number of classes which import the loger and configuration class
in the subfolder mymodule. What might be my mistake mistake?

Many thanks
Christian



### __init__.py ###

from  mymodule.MyLogger import MyLogger
from  mymodule.MyConfig import MyConfig


 
##### my_test.py ##########
from mymodule import MyConfig,MyLogger

#Both methods are static
key,logfile,loglevel = MyConfig().get_config('Logging')
log = MyLogger.set_logger(key,logfile,loglevel)
log.critical(time.time())

#Output
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19

[toc] | [next] | [standalone]


#45673

FromSkip Montanaro <skip@pobox.com>
Date2013-05-21 10:39 -0500
Message-ID<mailman.1926.1369150747.3114.python-list@python.org>
In reply to#45671

[Multipart message — attachments visible in raw view] — view raw

Don't confuse the use of "static" in Python with its use in C/C++.  From a
post on StackOverflow:

A staticmethod is a method that knows nothing about the class or instance
> it was called on. It just gets the arguments that were passed, no implicit
> first argument. It is basically useless in Python -- you can just use a
> module function instead of a staticmethod.


That is, the "@staticmethod" decorator doesn't mean, "call this function
once."

Skip

[toc] | [prev] | [next] | [standalone]


#45675

FromJohn Gordon <gordon@panix.com>
Date2013-05-21 16:34 +0000
Message-ID<kng7n6$efc$1@reader1.panix.com>
In reply to#45671
In <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Christian <mining.facts@gmail.com> writes:

> Hi,

> i'm somewhat confused working with @staticmethods. My logger and configuration  methods are called n times, but I have only one call.  
> n is number of classes which import the loger and configuration class
> in the subfolder mymodule. What might be my mistake mistake?

> Many thanks
> Christian

> ### __init__.py ###

> from  mymodule.MyLogger import MyLogger
> from  mymodule.MyConfig import MyConfig

> ##### my_test.py ##########
> from mymodule import MyConfig,MyLogger

> #Both methods are static
> key,logfile,loglevel = MyConfig().get_config('Logging')
> log = MyLogger.set_logger(key,logfile,loglevel)
> log.critical(time.time())

> #Output
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19

You haven't given us the code for your MyLogger class, so it's difficult
to say exactly what the problem is.

However, I have a guess.  Does MyLogger.set_logger() contain a call to
addHandler()?  Each call to addHandler() adds another handler to your
logger, and when you call log.critical() [or any other log function] you
get one line of output for each handler.

You should only call addHandler() once.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#45676

FromJohn Gordon <gordon@panix.com>
Date2013-05-21 16:48 +0000
Message-ID<kng8g7$lcp$1@reader1.panix.com>
In reply to#45675
In <kng7n6$efc$1@reader1.panix.com> John Gordon <gordon@panix.com> writes:

> You should only call addHandler() once.

...for each intended logging output destination, of course.  If you want
logging output to appear in a file and on-screen, then you would call
addHandler() once with a file handler and once with a screen handler.

But I think you may be calling addHandler multiple times for the same
file handler, which is causing the duplicate logging output.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#45678

FromChristian <mining.facts@gmail.com>
Date2013-05-21 10:17 -0700
Message-ID<42c56996-231f-476d-add8-375c584b77ee@googlegroups.com>
In reply to#45676
Am Dienstag, 21. Mai 2013 18:48:07 UTC+2 schrieb John Gordon:
> In <kng7n6$efc$1@reader1.panix.com> John Gordon <gordon@panix.com> writes:
> 
> 
> 
> > You should only call addHandler() once.
> 
> 
> 
> ...for each intended logging output destination, of course.  If you want
> 
> logging output to appear in a file and on-screen, then you would call
> 
> addHandler() once with a file handler and once with a screen handler.
> 
> 
> 
> But I think you may be calling addHandler multiple times for the same
> 
> file handler, which is causing the duplicate logging output.
> 
> 
> 
> -- 
> 
> John Gordon                   A is for Amy, who fell down the stairs
> 
> gordon@panix.com              B is for Basil, assaulted by bears
> 
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

Yes you're right.
Many thanks!

[toc] | [prev] | [next] | [standalone]


#45677

FromEthan Furman <ethan@stoneleaf.us>
Date2013-05-21 09:30 -0700
Message-ID<mailman.1928.1369155111.3114.python-list@python.org>
In reply to#45671
On 05/21/2013 08:39 AM, Skip Montanaro wrote:
> Don't confuse the use of "static" in Python with its use in C/C++.  From a post on StackOverflow:
>
>     A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the
>     arguments that were passed, no implicit first argument. It is basically useless in Python -- you can just use a
>     module function instead of a staticmethod.

For there record, staticmethod is useful when you want to make it possible for subclasses to change behavior.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#45693

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-05-21 18:23 -0700
Message-ID<83db731c-bc25-40cd-9271-815e11cb9088@googlegroups.com>
In reply to#45677
Ethan Furman於 2013年5月22日星期三UTC+8上午12時30分22秒寫道:
> On 05/21/2013 08:39 AM, Skip Montanaro wrote:
> 
> > Don't confuse the use of "static" in Python with its use in C/C++.  From a post on StackOverflow:
> 
> >
> 
> >     A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the
> 
> >     arguments that were passed, no implicit first argument. It is basically useless in Python -- you can just use a
> 
> >     module function instead of a staticmethod.
> 
> 
> 
> For there record, staticmethod is useful when you want to make it possible for subclasses to change behavior.
> 
> 
> 
> --
> 
> ~Ethan~

I prefer objects in classes with slimer figures not heavily weighted
with trivial methods in each instance construction and clone.

 But this is only my personal style of classes in python.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web