-1

I have a function and I want the first positional argument of that only give the instances of a specific class , a code like this in other programming languages

from channels import Channel


def get_frame_link(Channel channel):
    print(channel)

if coder use that function in this way the python most raise an error

get_frame_link(123)
get_frame_link("hello")

the truth way most be only this

channel=Channel()
get_frame_link(channel)

note: this is not duplicated question because I don't asked for how to check a variable type

yas17
  • 401
  • 1
  • 3
  • 14
  • 1
    well you did basically asked how to check a variable's type as there is no other way to "block" Python from accepting any type as an argument... You have type-hints, but those are just helpers and the user can still do whatever he wants – Tomerikoo Mar 08 '20 at 22:47

1 Answers1

1

Simply check that the type matches what you desire:

def get_frame_link(channel):
    if not isinstance(channel, Channel):
        raise TypeError("instance of type Channel expected")

    print(channel)
donkopotamus
  • 22,114
  • 2
  • 48
  • 60