0

I’m writing a very simple Python code which requires the minimum of two integers, so when I do min(2,5) I get TypeError: 'Operator' object is not callable error. However, when I do max(2,5) I get the correct output. I am trying this in a cell in Python notebook with Python3. The code I'm trying to execute:

def split(node_list):
    k, m = divmod(len(node_list), 5)
    chunks = [node_list[i*quo+min(i, m):(i+1)*k+min(i+1, m)] for i in range(5)]
    return chunks

From my research, some posts mentioned not to have any other variables named "min". Initially I was confused because I had no other variable or function named "min". Then I realized the the file I'm using is also executing code using gremlin_python library that has min() in-built function which is used to query Neptune graphs, and that's probably why I'm seeing this error. Could someone please let me know how I could explicitly ensure that the code used Python's in-build min()? Is there any other way to resolve this issue?

Edit: Gremlin imports i'm using

from gremlin_python import statics
from gremlin_python.driver import serializer
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.aiohttp.transport import AiohttpTransport
Sudha N
  • 23
  • 2
  • 2
    are you using a starred import? e.g. `from gremlin_python import *`? The solution is *not to do that* – juanpa.arrivillaga Jul 31 '23 at 18:19
  • in any case, somewhere, you have assigned to the variable `min`, whether it was with an assignment statement or with an import. we need a [mcve], or else we can only guess – juanpa.arrivillaga Jul 31 '23 at 18:28
  • So you should *really* fix whatever it is that is causing it. In any case, you can use `import builtins` and `builtins.min` to access the built-in `min`. But again, you **really** should fix whatever is causing this. It is a bad sign that you don't know where a variable is being assigned another value you didnt expect. – juanpa.arrivillaga Jul 31 '23 at 18:29
  • The Gremlin Python function can be referenced using `min_()` - Which version of Gremlin Python are you using? Functions like `min` were removed from Gremlin Python's static definitions some while back to avoid just these types of collision. – Kelvin Lawrence Jul 31 '23 at 18:37
  • I'm using gremlin python 3.6 – Sudha N Jul 31 '23 at 18:39
  • 1
    And how are you importing from gremlin? Its "min" function is going to be somewhere in the gremlin namespace, and it shouldn't be shadowing the builtin `min` unless you've imported it. – tdelaney Jul 31 '23 at 18:43
  • I'm also doing `statics.load_statics(globals())` so it looks like its importing gremlin's "min" function from there. I'm not sure if there's a better way to import these – Sudha N Jul 31 '23 at 18:54
  • Ah yes - don't do that! I think at some point the TinkerPop docs recommended it. But it's best not to. If you do for some reason need to keep doing that you will want to still `delete` the conflicting ones. But the best practice is to use the Gremlin specific function names when using Python as they are designed just to avoid such conflicts by adding an underscore to the name of any that do. – Kelvin Lawrence Jul 31 '23 at 20:18
  • using Gremlin specific function names worked, thank you! @KelvinLawrence – Sudha N Jul 31 '23 at 21:04

1 Answers1

1

Whenever a Gremlin Python step or function name collides with a Python reserved word, you can use an underscore after the function name. For example in gremlin Python you would use min_() .

In earlier versions of the TinkerPop documentation it was suggested to do statics.load_statics(globals()) which actually can cause exactly the issues you encountered. I would avoid messing with statics and just use the Gremlin form with the underscores.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38