1

I defined my class Player in Python3 and now I am trying to define a method in another class that would take an object type Player as a parameter, so a code would look like that:

def change_player_activity(self, player: Player):
    pass

But that gives me an error. I know that I could do a following operation:

if not isinstance(player,Player):
        raise TypeError

But I am just wondering, is there any built-in way to tell Python3 to accept only the objects of given class?

2
  • 1
    "But that gives me an error." What kind of error? Commented Oct 17, 2018 at 13:47
  • 3
    No. Python is not C or Java or any other statically-typed language. Type hinting are just, well, hints. You can check for the type with isinstance or if you really want to have a go with at it, with metaclasses Commented Oct 17, 2018 at 13:48

1 Answer 1

2

Is there any built-in way to tell Python3 to accept on the objects of a given class?

No.

This doesn't exist, and wouldn't be useful anyway because Python relies on the principle of Duck Typing. If it looks like a Player, and quacks like a Player, treat it as a player. Otherwise, raise TypeError.

You should only be raising TypeError when you ask the object to do something that a Player should be able to do, and it can't do that. What if somebody wants to pass an instance of their own Player class into your function? It won't work then.


Back to your actual problem: You want to type-hint Player before Player is defined. The way to do this is to write "Player" instead of Player in the type hint:

def change_player_activity(self, player: "Player"):
    pass

In __future__ (pun intended), the Python developers plan to make it not try to look up type hints straight away because this sort of thing is a pain.

Sign up to request clarification or add additional context in comments.

5 Comments

Depending on what you mean by "own Player class", that's already handled. An instance of a subclass of Player is still, in every meaningful sense, an instance of Player as well.
@chepner I mean one that isn't a subclass; there are reasons to do this, such as to avoid metaclass hideousness.
There would be equally valid reasons to believe that such a class doesn't quack enough like a "real" Player to be considered a valid substitute.
@chepner In which case some call or other will fail. Then you can raise a TypeError. Having a slow check for isinstance every call is not very efficient.
I think we agree. If isinstance is used, it should be done to affect how the function handles the argument, not simply to decide whether or not to raise a TypeError (or some other exception). I'm of the opinion that in a dynamically typed language, it's the caller's responsibility to pass values of the correct type, rather than the callee's responsibility to reject values of the wrong type. Type hints are a tool to help in the latter case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.