Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

@staticmethods called more than once

21 views
Skip to first unread message

Christian

unread,
May 21, 2013, 11:26:46 AM5/21/13
to
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

Skip Montanaro

unread,
May 21, 2013, 11:39:03 AM5/21/13
to Christian, pytho...@python.org
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
 

John Gordon

unread,
May 21, 2013, 12:34:46 PM5/21/13
to
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
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

John Gordon

unread,
May 21, 2013, 12:48:07 PM5/21/13
to
In <kng7n6$efc$1...@reader1.panix.com> John Gordon <gor...@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.

Ethan Furman

unread,
May 21, 2013, 12:30:22 PM5/21/13
to pytho...@python.org
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~

Christian

unread,
May 21, 2013, 1:17:53 PM5/21/13
to
Yes you're right.
Many thanks!

88888 Dihedral

unread,
May 21, 2013, 9:23:14 PM5/21/13
to
Ethan Furman於 2013年5月22日星期三UTC+8上午12時30分22秒寫道:
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.
0 new messages