0

I just started with coding in python, at the moment I am trying to define a function that needs an object as input. Before this I just developed in Java.

def welcome_message(c):
    msg.send("Hi %s", c.first_name)

So this function works, I was wondering if I still should insert a constraint that c has to be on object of the type Customer? It just doesn't really feel safe for a java experienced developer...

hY8vVpf3tyR57Xib
  • 2,946
  • 4
  • 35
  • 73
  • 2
    Welcome to the world of dynamic typing! Why should the function care if `c` is a `Customer` object? Anything with that attribute should suffice, right? – Martijn Pieters Aug 05 '15 at 20:35

2 Answers2

0

The python way of thinking is that the programmer or those that use your software will be intelligent enough to use it properly so long as you've written and documented your software clearly. More that likely when passing in the object, c.first_name will barf if it's not the right type. One way to protect things is the following:

def welcome_message(c):
    if isinstance(c, Customer):
        msg.send("Hi %s", c.first_name)
    else:
        raise TypeError("Class must be of type `Customer`")

However, I would recommend not doing this, unless it's absolutely necessary.

James Mertz
  • 7,493
  • 9
  • 53
  • 84
  • Don't sprinkle your code with `isintance()` checks. If you *have to* use types; you could [use type hints](https://docs.python.org/3.5/whatsnew/3.5.html#pep-484-type-hints): `welcome_message(c: Customer)`, [docstrings](https://www.jetbrains.com/pycharm/help/type-hinting-in-pycharm.html), and/or a [decorator](http://stackoverflow.com/q/15299878/4279) (for Python 2 compatibility and/or run-time check). – jfs Aug 05 '15 at 22:00
  • @J.F.Sebastian Python 3.5 hasn't been officially released yet. Still in beta. Docstrings is more specific to what I meant by `documenting your software clearly`. Also I'm not recommending the OP use this unless entirely necessary. – James Mertz Aug 05 '15 at 22:33
  • you don't need Python 3.5 to use type hints (lookup `mypy-lang`). You don't need Python 3.5 to put type info into docstrings in the format that your IDE understands. You don't need Python 3.5 to use a type checking decorator. – jfs Aug 05 '15 at 22:40
  • According to [PEP-484](https://www.python.org/dev/peps/pep-0484/) type hints was accepted and instituted in Python 3.5. I didn't take the time to check to see if it works with my Python 3.2 install. The rest of my comments weren't Python 3.5 releated. – James Mertz Aug 05 '15 at 22:47
  • PEP-484 mentions mypy explicitly that claims to work on Python 3.2+. Obviously, there could be other tools that understand function annotations for various purposes in addition to static/dynamic type checking e.g., attribute suggestions on typing dot in IDE. – jfs Aug 05 '15 at 23:07
  • @J.F.Sebastian I just checked and it does work on Python 3.4. However, I couldn't find any documentation supporting type hints anywhere other than in Python 3.5. – James Mertz Aug 05 '15 at 23:11
-1

If an object without a first_name attribute is passed, this will fail. So put that function body in a try ... except and decide what you want to do when it fails.

Alan
  • 8,925
  • 10
  • 18
  • 1
    No, do not put in a `try...except`. If an object is passed in without such an attribute, that'd be a *bug*. It is important that the code fails with a proper traceback in that case. – Martijn Pieters Aug 05 '15 at 20:36
  • @MartijnPieters You can always `raise` if that's what "you want to do". (See the answer.) Whether you should or not depends on the goals in your program. Simply failing is not always an option. – Alan Aug 05 '15 at 22:06
  • Catching the exception should only be used where not doing so is not an option. In the vast majority of cases, catching it inside this function is not a good idea. – Martijn Pieters Aug 05 '15 at 22:08