language-features

Python type() or __class__, == or is

Python type() or __class__, == or is Question: I want to test whether an object is an instance of a class, and only this class (no subclasses). I could do it either with: obj.__class__ == Foo obj.__class__ is Foo type(obj) == Foo type(obj) is Foo Are there reasons to choose one over another? (performance differences, …

Total answers: 4

Reading a line from standard input in Python

Reading a line from standard input in Python Question: What (if any) are the differences between the following two methods of reading a line from standard input: raw_input() and sys.stdin.readline() ? And in which cases one of these methods is preferable over the other ? Asked By: Grigor Gevorgyan || Source Answers: “However, from the …

Total answers: 2

Python: How to pass more than one argument to the property getter?

Python: How to pass more than one argument to the property getter? Question: Consider the following example: class A: @property def x(self): return 5 So, of course calling the a = A(); a.x will return 5 But imagine that you want to be able to modify the property x. This way, for example: class A: …

Total answers: 7

Python ? (conditional/ternary) operator for assignments

Python ? (conditional/ternary) operator for assignments Question: C and many other languages have a conditional (AKA ternary) operator. This allows you to make very terse choices between two values based on the truth of a condition, which makes expressions, including assignments, very concise. I miss this because I find that my code has lots of …

Total answers: 2

What is the python "with" statement designed for?

What is the python "with" statement designed for? Question: I came across the Python with statement for the first time today. I’ve been using Python lightly for several months and didn’t even know of its existence! Given its somewhat obscure status, I thought it would be worth asking: What is the Python with statement designed …

Total answers: 11

Why doesn't a python dict.update() return the object?

Why doesn't a python dict.update() return the object? Question: I have this code: award_dict = { "url": "http://facebook.com", "imageurl": "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count": 1, } def award(name, count, points, desc_string, my_size, parent): if my_size > count: a = { "name": name, "description": desc_string % count, "points": points, "parent_award": parent, } a.update(award_dict) return self.add_award(a, siteAlias, alias).award But the …

Total answers: 11

What is the purpose of python's inner classes?

What is the purpose of python's inner classes? Question: Python’s inner/nested classes confuse me. Is there something that can’t be accomplished without them? If so, what is that thing? Asked By: Geo || Source Answers: Quoted from http://www.geekinterview.com/question_details/64739: Advantages of inner class: Logical grouping of classes: If a class is useful to only one other …

Total answers: 9

What is a maximum number of arguments in a Python function?

What is a maximum number of arguments in a Python function? Question: It’s somewhat common knowledge that Python functions can have a maximum of 256 arguments. What I’m curious to know is if this limit applies to *args and **kwargs when they’re unrolled in the following manner: items = [1,2,3,4,5,6] def do_something(*items): pass I ask …

Total answers: 6