Set static attributes...maybe.

Chris Kaynor ckaynor at zindagigames.com
Mon Aug 18 13:13:08 EDT 2014


On Mon, Aug 18, 2014 at 10:00 AM, Beppe <giuseppecostanzi at gmail.com> wrote:

> hi to everybody,
> in the following scrip I try to call the function read_parameters () but
> returns me that wants two arguments!?!?!
> My intent is to have in the class, Engine (), some static attributes that
> can be used by  other instances without to redefine this every time.
> These matters could be for example a path or a connection to a database.
> suggestions?
>
> regards
> beppe
>
> class Master(object):
>     def __init__(self,):
>         pass
>
> class Engine(Master):
>     dict_parameters = {}
>     def __init__(self,):
>         super(Engine, self).__init__()
>
>     @staticmethod
>     def read_parameters(self,path):
>
>         self.dict_parameters = {1:"a",2:"b"}
>

What you probably want here, based on your description is (untested):
@classmethod
def read_parameters(cls, path): # Note, the name is "cls". This is not
required, but is convention, similar to "self".
    cls.dict_parameters = {1:"a",2:"b"}

@staticmethod creates a method that does not receive any special parameter,
so the signature would be "def read_parameters(path)".

Note that, personally, I would name the method "parse_parameters" to make
it clearer what it does.


>
>     def check_parameters(self):
>         self.read_parameters("hello_world")


>
> foo=Engine()
> foo.check_parameters()
>
> p.s.
> I'm on Debian 6
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140818/e6cfbcba/attachment.html>


More information about the Python-list mailing list