Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to declare a function argument that can accept any type in Python

3 Answers

0 votes
def AcceptAnyType(x):
    print(f"The type of x is: {type(x)}")
    if type(x) == int:
        print("x is an integer.")
    elif type(x) == float:
        print("x is a float.")
    elif type(x) == bool:
        print("x is a bool.")    
    elif type(x) == str:
        print("x is a string.")
    else:
        print("x is of another type.")


AcceptAnyType(9001)
AcceptAnyType(3.14)
AcceptAnyType('a')
AcceptAnyType("XYZ")
AcceptAnyType(True)


  
'''
run:
  
The type of x is: <class 'int'>
x is an integer.
The type of x is: <class 'float'>
x is a float.
The type of x is: <class 'str'>
x is a string.
The type of x is: <class 'str'>
x is a string.
The type of x is: <class 'bool'>
x is a bool.

'''

 



answered Aug 1, 2025 by avibootz
0 votes
def AcceptAnyType(x):
    if isinstance(x, int):
        print("x is an integer.")
    elif isinstance(x, float):
        print("x is a float.")
    elif isinstance(x, bool):
        print("x is a bool.")
    elif isinstance(x, str):
        print("x is a string.")
    else:
        print(f"x is of type: {type(x)}")


AcceptAnyType(9001)
AcceptAnyType(3.14)
AcceptAnyType('a')
AcceptAnyType("XYZ")
AcceptAnyType(True)


  
'''
run:
  
x is an integer.
x is a float.
x is a string.
x is a string.
x is an integer.

'''

 



answered Aug 1, 2025 by avibootz
0 votes
from typing import Any

def AcceptAnyType(x: Any):
    if isinstance(x, list):
        print("x is a list.")
    elif isinstance(x, dict):
        print("x is a dictionary.")
    else:
        print(f"x is of type: {type(x)}")


AcceptAnyType({1: "C", 2: "Java", 3: "Python",})
AcceptAnyType([1, 2, 3])
AcceptAnyType(9001)
AcceptAnyType(3.14)
AcceptAnyType('a')
AcceptAnyType("XYZ")
AcceptAnyType(True)


  
'''
run:
  
x is a dictionary.
x is a list.
x is of type: <class 'int'>
x is of type: <class 'float'>
x is of type: <class 'str'>
x is of type: <class 'str'>
x is of type: <class 'bool'>

'''

 



answered Aug 1, 2025 by avibootz
...