python-2.x

f-strings giving SyntaxError?

被刻印的时光 ゝ 提交于 2019-12-17 06:51:50
问题 I am getting an error message with my Atom reader here, where it is suggesting the first print.(f"message") is delivering an error: File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75 print(f"Let's talk about {my_name}.") ^ SyntaxError: invalid syntax [Finished in 0.077s] Code: my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches my_weight = 180 #lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' print(f"Let's talk about {my_name}.") print(f"He

f-strings giving SyntaxError?

我的梦境 提交于 2019-12-17 06:51:07
问题 I am getting an error message with my Atom reader here, where it is suggesting the first print.(f"message") is delivering an error: File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75 print(f"Let's talk about {my_name}.") ^ SyntaxError: invalid syntax [Finished in 0.077s] Code: my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches my_weight = 180 #lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' print(f"Let's talk about {my_name}.") print(f"He

How to select a directory and store the location using tkinter in Python

拥有回忆 提交于 2019-12-17 06:36:28
问题 I am creating a GUI with a browse button which I only want to return the path. I've been looking at solutions using code like below. Tkinter.Button(subframe, text = "Browse", command = self.loadtemplate, width = 10).pack() def loadtemplate(self): filename = tkFileDialog.askopenfilename(filetypes = (("Template files", "*.tplate") ,("HTML files", "*.html;*.htm") ,("All files", "*.*") )) if filename: try: self.settings["template"].set(filename) except: tkMessageBox.showerror("Open Source File",

Defining “boolness” of a class in python

狂风中的少年 提交于 2019-12-17 05:00:31
问题 Why doesn't this work as one may have naively expected? class Foo(object): def __init__(self): self.bar = 3 def __bool__(self): return self.bar > 10 foo = Foo() if foo: print 'x' else: print 'y' (The output is x ) 回答1: For Python 2-3 compatibility, just add this to your example: Foo.__nonzero__ = Foo.__bool__ or expand the original definition of Foo to include: __nonzero__ = __bool__ You could of course define them in reverse too, where the method name is __nonzero__ and you assign it to _

Defining “boolness” of a class in python

拥有回忆 提交于 2019-12-17 05:00:03
问题 Why doesn't this work as one may have naively expected? class Foo(object): def __init__(self): self.bar = 3 def __bool__(self): return self.bar > 10 foo = Foo() if foo: print 'x' else: print 'y' (The output is x ) 回答1: For Python 2-3 compatibility, just add this to your example: Foo.__nonzero__ = Foo.__bool__ or expand the original definition of Foo to include: __nonzero__ = __bool__ You could of course define them in reverse too, where the method name is __nonzero__ and you assign it to _

Safest way to convert float to integer in python?

瘦欲@ 提交于 2019-12-17 04:26:22
问题 Python's math module contain handy functions like floor & ceil . These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import math f=math.floor(2.3) Now f returns: 2.0 What is the safest way to get an integer out of this float, without running the risk of rounding errors (for example if the float is the equivalent of 1.99999) or perhaps I should use another function

Safest way to convert float to integer in python?

左心房为你撑大大i 提交于 2019-12-17 04:26:20
问题 Python's math module contain handy functions like floor & ceil . These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import math f=math.floor(2.3) Now f returns: 2.0 What is the safest way to get an integer out of this float, without running the risk of rounding errors (for example if the float is the equivalent of 1.99999) or perhaps I should use another function

Safest way to convert float to integer in python?

ε祈祈猫儿з 提交于 2019-12-17 04:26:01
问题 Python's math module contain handy functions like floor & ceil . These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import math f=math.floor(2.3) Now f returns: 2.0 What is the safest way to get an integer out of this float, without running the risk of rounding errors (for example if the float is the equivalent of 1.99999) or perhaps I should use another function

Python exception chaining [duplicate]

China☆狼群 提交于 2019-12-17 04:21:14
问题 This question already has answers here : “Inner exception” (with traceback) in Python? (9 answers) Closed 4 years ago . Is there a standard way of using exception chains in Python? Like the Java exception 'caused by'? Here is some background. I have a module with one main exception class DSError : class DSError(Exception): pass Somewhere within this module there will be: try: v = my_dict[k] something(v) except KeyError as e: raise DSError("no key %s found for %s" % (k, self)) except

Using print() (the function version) in Python2.x

元气小坏坏 提交于 2019-12-17 02:49:10
问题 I understand the difference between a statement and an expression, and I understand that Python3 turned print() into a function. However I ran a print() statement surrounded with parenthesis on various Python2.x interpreters and it ran flawlessly, I didn't even have to import any module. My question: Is the following code print("Hello SO!") evaluated as a statement or an expression in Python2.x? 回答1: Consider the following expressions: a = ("Hello SO!") a = "Hello SO!" They're equivalent. In