python-3.5

Why is x**4.0 faster than x**4 in Python 3?

Why is x**4.0 faster than x**4 in Python 3? Question: Why is x**4.0 faster than x**4? I am using CPython 3.5.2. $ python -m timeit “for x in range(100):” ” x**4.0″ 10000 loops, best of 3: 24.2 usec per loop $ python -m timeit “for x in range(100):” ” x**4″ 10000 loops, best of 3: …

Total answers: 3

The correct way to annotate a "file type" in Python

The correct way to annotate a "file type" in Python Question: In modern versions of Python one can have static type analysis using function annotations, according to PEP 484. This is made easy through the typing module. Now I’m wondering how I would give a "type hint" towards a "filestream". def myfunction(file: FILETYPE): pass with …

Total answers: 4

Unpacking generalizations

Unpacking generalizations Question: PEP 448 — Additional Unpacking Generalizations allowed: >>> LOL = [[1, 2], [‘three’]] >>> [*LOL[0], *LOL[1]] [1, 2, ‘three’] Alright! Goodbye itertools.chain. Never liked you much anyway. >>> [*L for L in LOL] File "<ipython-input-21-e86d2c09c33f>", line 1 [*L for L in LOL] ^ SyntaxError: iterable unpacking cannot be used in comprehension Oh. …

Total answers: 2

How do I specify OrderedDict K,V types for Mypy type annotation?

How do I specify OrderedDict K,V types for Mypy type annotation? Question: I am using Python 3.5 together with Mypy to have some basic static checking for my script. Recently I refactored some methods to return OrderedDict, but ran into “‘type’ object is not subscriptable” error, when I tried to use return annotation with Key …

Total answers: 5

How to use asyncio with existing blocking library?

How to use asyncio with existing blocking library? Question: I have few blocking functions foo, bar and I can’t change those (Some internal library I don’t control. Talks to one or more network services). How do I use it as async?. E.g. I wan’t to do the following. results = [] for inp in inps: …

Total answers: 4

@asyncio.coroutine vs async def

@asyncio.coroutine vs async def Question: With the asyncio library I’ve seen, @asyncio.coroutine def function(): … and async def function(): … used interchangeably. Is there any functional difference between the two? Asked By: Jason || Source Answers: async def is a new syntax from Python 3.5. You could use await, async with and async for inside …

Total answers: 3

Labels not defined in tkinter app

Labels not defined in tkinter app Question: I’m trying to make a basic window with the text “t” inside using Tkinter, however when running the code the shell spits out “NameError: name ‘Label’ is not defined”. I’m running Python 3.5.2. I followed the tutorials but the problem is in the label = Label(root, text=”test”) line. …

Total answers: 6

Python 3.5 async/await with real code example

Python 3.5 async/await with real code example Question: I’ve read tons of articles and tutorial about Python’s 3.5 async/await thing. I have to say I’m pretty confused, because some use get_event_loop() and run_until_complete(), some use ensure_future(), some use asyncio.wait(), and some use call_soon(). It seems like I have a lot choices, but I have no …

Total answers: 2

typing.Any vs object?

typing.Any vs object? Question: Is there any difference between using typing.Any as opposed to object in typing? For example: def get_item(L: list, i: int) -> typing.Any: return L[i] Compared to: def get_item(L: list, i: int) -> object: return L[i] Asked By: Markus Meskanen || Source Answers: Yes, there is a difference. Although in Python 3, …

Total answers: 2

Python type hinting without cyclic imports

Python type hinting without cyclic imports Question: I’m trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so: main.py file: import mymixin.py class Main(object, MyMixin): def func1(self, xxx): … mymixin.py file: class MyMixin(object): def func2(self: Main, xxx): # <— note the type hint …

Total answers: 8