Python Argument Type Error
Contents |
This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the exceptions module. For class exceptions, in a try statement with an python typeerror except clause that mentions a particular class, that clause also handles any exception python filenotfounderror classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not
Python Custom Exception
related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an
Python Exception Message
"associated value" indicating the detailed cause of the error. This may be a string or a tuple containing several items of information (e.g., an error code and a string explaining the code). The associated value is the second argument to the raise statement. If the exception class is derived from the standard root class BaseException, the associated value is present as the exception instance's args python valueerror example attribute. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. The following exceptions are only used as base classes for other exceptions. exception BaseException¶ The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() or unicode() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments. New in version 2.5. args¶ The tuple of arguments given to the exception constructor. Some built-in exceptions (like IOError) expect a certain number of arguments and assign a special meaning to the elements
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
Python 3 Exceptions
site About Us Learn more about Stack Overflow the company Business Learn more python exception class methods about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x assertionerror python Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Which exception https://docs.python.org/2/library/exceptions.html should I raise on bad/illegal argument combinations in Python? up vote 236 down vote favorite 26 I was wondering about the best practices for indicating invalid argument combinations in Python. I've come across a few situations where you have a function like so: def import_to_orm(name, save=False, recurse=False): """ :param name: Name of some external entity to import. :param save: Save the ORM object before http://stackoverflow.com/questions/256222/which-exception-should-i-raise-on-bad-illegal-argument-combinations-in-python returning. :param recurse: Attempt to import associated objects as well. Because you need the original object to have a key to relate to, save must be `True` for recurse to be `True`. :raise BadValueError: If `recurse and not save`. :return: The ORM object. """ pass The only annoyance with this is that every package has its own, usually slightly differing BadValueError. I know that in Java there exists java.lang.IllegalArgumentException -- is it well understood that everybody will be creating their own BadValueErrors in Python or is there another, preferred method? python exception arguments share|improve this question edited Nov 2 '11 at 19:26 sth 128k33204309 asked Nov 1 '08 at 23:02 cdleary 27.4k40130178 add a comment| 4 Answers 4 active oldest votes up vote 245 down vote accepted I would just raise ValueError, unless you need a more specific exception.. def import_to_orm(name, save=False, recurse=False): if recurse and not save: raise ValueError("save must be True if recurse is True") There's really no point in doing class BadValueError(ValueError):pass - your custom class is identical in use to ValueError, so why not use that? share|improve this answer edited Apr 29 at 18:04 izrik 38629 a
here for a quick overview of the site Help Center Detailed answers to any questions you might http://stackoverflow.com/questions/14414720/python-typeerror-argument-of-type-builtin-function-or-method-is-not-iterable have Meta Discuss the workings and policies of this site http://stackoverflow.com/questions/2489669/function-parameter-types-in-python About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 python exception million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python: TypeError: argument of type 'builtin_function_or_method' is not iterable up vote 5 down vote favorite I have the following code: def search(): os.chdir("C:/Users/Luke/Desktop/MyFiles") files = os.listdir(".") os.mkdir("C:/Users/Luke/Desktop/FilesWithString") string = input("Please enter the website your are looking for (in python argument type lower case):") for x in files: inputFile = open(x, "r") try: content = inputFile.read().lower except UnicodeDecodeError: continue inputFile.close() if string in content: shutil.copy(x, "C:/Users/Luke/Desktop/FilesWithString") which always gives this error: line 80, in search if string in content: TypeError: argument of type 'builtin_function_or_method' is not iterable can someone shed some light on why. thans python python-3.x share|improve this question asked Jan 19 '13 at 13:09 user1816467 add a comment| 2 Answers 2 active oldest votes up vote 17 down vote accepted Change the line content = inputFile.read().lower to content = inputFile.read().lower() Your original line assigns the built-in function lower to your variable content instead of calling the function str.lower and assigning the return value which is definitely not iterable. share|improve this answer answered Jan 19 '13 at 13:18 Abhijit 36.3k649115 got it, thanks a lot –user1816467 Jan 19 '13 at 13:21 @LWH91: Don't forget to accept and upvote the answer that helped. –Abhijit Jan 19 '13 at 13:22
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Function parameter types in Python up vote 77 down vote favorite 33 Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2): # stuff However, you don't actually give the types of those parameters. Also, if I remember, Python is a strongly typed language, as such, it seems like Python shouldn't let you pass in a parameter of a different type than the function creator expected. However, how does Python know that the user of the function is passing in the proper types? Will the program just die if it's the wrong type, assuming the function actually uses the parameter? Do you have to specify the type? python function parameters share|improve this question edited Sep 23 '12 at 14:21 efotinis 9,33141831 asked Mar 22 '10 at 2:17 Leif Andersen 7,91984574 add a comment| 10 Answers 10 active oldest votes up vote 74 down vote accepted Python is strongly typed because every object has a type, every object knows its type, it's impossible to accidentally or deliberately use an object of a type "as if" it was an object of a different type, and all elementary operations on the object are delegated to its type. This has nothing to do with names. A name in Python doesn't "have a type": if and when a name's defined, the name refers to an object, and the object does have a type (but that doesn't in fact force a type on the name: a name is a name). A name in Python can perfectly well refer to different objects at differen