2

I know that this question will recieve some fairly strong comments but I'm going to post it anyway.

I'm working on a project in Python. My simple reasons for choosing Python are the available librtaries and it is cross platform and open source. My problem is that I really enjoy coding with the very verbose, descriptive method names of Objective C. The type of functions that I have to write can have fairly complex arguments and I find that with the Objective C approach I'm not having to constantly refer back to other pieces of dicumentation to make sure I'm using the right arguments with the right functions.

Is there any way of using the Objective C style for calling functions with Python? I am aware this probably doesn't suit recommended coding styles for Python, but it does suit what I am doing right now so I'd like to try and find a way of doing it if possible.

Ian Turner
  • 1,363
  • 1
  • 15
  • 27

1 Answers1

4

If you mean, "can I write the call with square brackets instead of round ones and put them around the object and method names instead of after and omit the commas", then no.

If you mean "can I make verbose, descriptive method names", then of course you can; you can do that in any language worth mentioning.

I think what you really mean is "can I use keyword arguments as in [myObject frobnicateWithHax:42 Foo:23 Bar:69]?". Yes; in Python, this is spelled myObject.frobnicateWith(hax=42, foo=23, bar=69). On the function-definition side, there are a number of ways to make it work, depending on exactly what you want; see the documentation (or a good reference or tutorial) for default arguments and kwargs.

Honestly, Google answers this kind of question better than SO, once you know a little bit about what these kinds of language features are called.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • @Rudy See [this question](http://stackoverflow.com/questions/3394835/args-and-kwargs) – Lauritz V. Thaulow Aug 01 '11 at 10:42
  • 1
    Note that in Python you'd almost certainly drop the `With` in the name, and just call `my_object.frobnicate(hax=42, foo=23, bar=69)`. Note that your function definition doesn't need to use defaults or kwargs -- just the names are enough, for example: `def frobnicate(self, hax, foo, bar): ...` – Ben Hoyt Aug 01 '11 at 14:24
  • I mentioned Google for a reason... try 'python kwargs' and see what you get. @ben this is all true, at least assuming you don't want to **force** the caller to specify the names. :) Which wouldn't be very Pythonic :) – Karl Knechtel Aug 01 '11 at 21:46