Is there any way to pass the parameters to a constant function?

Asked

Viewed 144 times

5

I’m trying to do the following, I have this connection to Influxdb:

InfluxDBClient('localhost', 8086, 'root', 'root', 'example')

But I want to turn these parameters into a constant, so I don’t have to keep changing the code every time I change.

At first I thought to create for each parameter:

INFLUXDB_HOST = 'influxdb'
INFLUXDB_PORT = 8086
INFLUXDB_USERNAME = 'root'
INFLUXDB_PASSWORD = 'root'
INFLUXDB_DATABASE = 'example'

But does it have a way to join all these variables within one and pass straight into the function?

InfluxDBClient(MINHAS_VARIAVEIS)

Influx is not the problem itself, the same case would be for the example below.

In [1]: def conta(a, b, c):
   ...:     print(a,b,c)
   ...:     

In [2]: conta(1,2,3)
(1, 2, 3)

In [3]: teste = (1,2,3)

In [4]: conta(teste)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-5feac543a26b> in <module>()
----> 1 conta(teste)

TypeError: conta() takes exactly 3 arguments (1 given)

In [5]: 

In case I would not change inside the function (because in Influx there is no way).

  • 1

    The function InfluxDBClient is yours? You can change?

  • No, the only constraint is not changing the function, otherwise I would make a tuple or list.

3 answers

7


You could declare your variables on a list, and then use * to pass each item as an argument

CONFIG = ('localhost', 8086, 'root', 'root', 'example')
InfluxDBClient(*CONFIG)

You could create a function that calls the Influxdbclient by passing the required parameters

def localhostConnect(): return InfluxDBClient('localhost', 8086, 'root', 'root', 'example')
  • 1

    I didn’t know there was such a * in python

6

It has some forms, but it doesn’t seem to be quite what you want. I am assuming that you can and will change the function to work with a different parameter than you currently accept, even if it is another function. If you cannot do this no what to do, you will have to write all variables, which is not the end of the world. Your options are:

  • Receive a list (or dictionary, or tuple) with these values. Instead of having loose variables you would have a list. This can run in two ways:
    • Normally receive as a parameter
    • Receive as a list of parameters using * (see more)
  • No more parameter and there are two options:
    • Leave the variables as global (usually not recommended, but there are useful cases), which can then be accessed from anywhere and already picks up the data inside
    • It creates a function that returns these values in a list or dictionary or tuple, so whenever you change the value you have to change in this function, and within its consumer function you call this function and take the data. So it gets a little more organized and only the function becomes global and not the variables.

What you can do if you can’t change the function is create abstraction, what I always say, you call the function you want the way you want it within a function, and then always call this simpler function. Abstraction is a lost art that ancient programmers have always used easily to hide complexity. Then came object orientation and people stopped thinking about it, only now they know how to create complex solutions.

Not having access to the original function can do this in another one where it accesses the global variables (which is what it seems to want) directly there, so it no longer has to type anything extra if this is the concern. Example:

def dbConnect(): return InfluxDBClient(INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD = 'root', INFLUXDB_DATABASE)

I put in the Github for future reference.

1

you can encapsulate your function

 def novoNome(ParametroMutavel):
    return oldFunction(Parametrofixo, ParametroMutavel)

Example with your code

def newFunction():
    return InfluxDBClient('localhost', 8086, 'root', 'root', 'example')

Suppose the example always changes

def newFunction(parametroMutavel):
    return InfluxDBClient('localhost', 8086, 'root', 'root', parametroMutavel)

Suppose you have many changeable parametres

obj = {
    'ip':'localhost',
    'port':8086,
    'user':'root',
    'pw':'root',
    'ex':'example',
}
def newFunction(dicionario):
    return InfluxDBClient(dicionario.get('ip'), dicionario.get('port'), dicionario.get('user'), dicionario.get('pw'), dicionario.get('ex'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.