1

I have two functions with long names:

# ruby
def test_write_method_raises_value_error

# python
def test_write_method_raises_value_error(self):

In ruby I can rename the function as follows:

test 'write method should raise value error' do
  # rest of function

Is there a comparable construct in Python?

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
  • 1
    What you could do is saving the method in a variable. – Vincent Beltman Oct 27 '14 at 13:04
  • You can remove it but pep8 says its not python style. Please check this http://legacy.python.org/dev/peps/pep-0008/#function-names. Python will not give error but its not its flavor. – Nilesh Oct 27 '14 at 13:06
  • possible duplicate of [What is the naming convention in Python for variable and function names?](http://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names) – l'L'l Oct 27 '14 at 13:07
  • That's not renaming the function; I don't know Ruby well, but that looks like the syntax for calling a block on each item returned from a call to `test`. You could simulate that in Python (within the constraints of syntax), but as it's not idiomatic, I doubt you'd want to. – chepner Oct 27 '14 at 13:45

1 Answers1

1

If you just need to make name of function shorter for testing purposes, it can be easily done.

Everything is object in python, even functions. And you can assign it to the variable, which is in fact just a label.

For example:

def very_very_long_function_name_that_we_want_to_test(argument):
     pass


# test:
testable = very_very_long_function_name_that_we_want_to_test

# This will call our "long function"
testable('test')

But new name of this function should be correct according to Python naming constraints, no spaces for instance

Nikolai Golub
  • 3,327
  • 4
  • 31
  • 61