Python is a dynamic, high-level language. Its design focuses on clear syntax, an intuitive approach to object-oriented programming, and making the right way to do things obvious. Python supports modules, exceptions, had an extensive standard module library. Python can also be embedded in other ...

learn more… | top users | synonyms (1)

1
vote
0answers
103 views

Is the logic behind `Asyncio.wait()` and async/await, the same, just the code is written differently (syntax)?

I'm learning Python, more specially parallel programming using Python Parallel Programming Cookbook by Giancarlo Zaccone. At the time the book was published async/await was still in the beta version ...
5
votes
3answers
455 views

Why do you need “self.” in Python to refer to instance variables?

I have been programming into a number of languages like Java, Ruby, Haskell and Python. I have to switch between many languages per day due to different projects I work on. Now, the issue is I often ...
0
votes
2answers
189 views

Does Python have any features which can be used for encapsulating private data?

Usually in OOP world we are told that modularity is a good practice and keeping loose coupling between module is a great thing. Encapsulation helps us achieve this loose coupling. In Java ...
4
votes
1answer
92 views

Enclosing main invocation of a function in an if

As a part of my school CS class, I am reading Python Programming by John Zelle. In the book, Zelle talks about surrounding the invocation of a main method with if <condition>: main() ...
4
votes
2answers
99 views

Sort a list while putting together or after?

I have to read through an extremely large amount of network data from various log files, and compile relevant information about that data to perform statistical analysis on it (the top communicators, ...
6
votes
2answers
749 views

Bad practice to define constants based on a function call?

In Python when writing file management scripts I will often have a base path that is a constant, BASE = "C:/" Of course I'll be using that base path to create other paths later on, including other ...
0
votes
0answers
79 views

Why use Python's asyncio with coroutines to implement a server

I use Python's asyncio library to create a server that can handle telnet requests. Because asyncio's server loop is single threaded, I thought it would make more sense that when asyncio tells me that ...
0
votes
1answer
88 views

Parsing conditional statements

I've written a small utility in Python3 to help me copy my music collection from my NAS to a mobile device. The usefulness of this is that it will auto-convert flac files to ogg-vorbis (to save space) ...
0
votes
1answer
86 views

What i and n stand for in python? [duplicate]

I've been writing a program to pick random words from my list. However, to do that I had to imitate some solutions on the internet, and I succeeded. Unfortunately, there is something that I can't ...
0
votes
1answer
94 views

Python - Cache function and decorator

I am playing with cache functions using decorators. First, I use a generic function def memorize(func): cache = {} print "printing cache" print cache print "cache printed" ...
-1
votes
3answers
125 views

Python — Class and Object [closed]

I got a question that quite disturbs me a lot and I think it might help a lot if I had an answer to it. So I got this: class Klasse1: variable1 = "haha" class Klasse2: variable2 = "hoho" ...
2
votes
1answer
82 views

Why is Python sorted() result like this?

I might be missing something obvious here, but why does Python 2.x sorted() function work like this? In [95]: l = [1, '5', 4, 2, '3', 'b', 'c', '-a'] In [96]: sorted(l) Out[96]: [1, 2, 4, '-a', '3', ...
0
votes
1answer
43 views

Are there any pitfalls when replacing a Python module using sys.modules?

In a current project of mine, I have decided to not put any significant amount of code in __init__.py files, simply because I don't like them. In my head, an __init__.py file is just there to inform ...
2
votes
1answer
76 views

How does the Python runtime know which object a method is bound to?

When defining methods in a class we do have an argument for the object on which the method is invoked i.e self. Say I have a class Foo. class Foo: def m(self): pass now when we have an ...
0
votes
1answer
88 views

How to do reports from python [closed]

This is not a question, asking for a (single) tool, although to answer my question, of course tools are involved. The focus of this question is, asking for advice in which direction to head for. I ...
0
votes
2answers
66 views

What is the difference between proxy class and delegation in Python?

Wiki: A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, ...
5
votes
2answers
399 views

How are scripting languages compiled?

I know the term "scripting languages" is just a subset of programming languages, but I want to refer to programming languages such as Python and Ruby among others. First of all, why don't we need a ...
7
votes
2answers
2k views

how to refactor many singletons

I have a medium-sized python program (~5000 lines of code), which I've built up over time, with no particular plan as I went ahead. The architecture I've ended up with consists of 5-6 large Singleton ...
1
vote
0answers
49 views

Assess the quality of third party python packages

I'm developing a application in Python and I've found various packages on pypi which appear to be what I need. However, how do I know I can trust these lesser known packages - it only has a couple ...
0
votes
2answers
406 views

How does the communication between a chat server and client work?

(Socket programming newbie here...) So, for learning purposes I've just started developing a chat server and client in Python, and I was wondering how it is usually implemented. When someone ...
2
votes
0answers
81 views

Eliminate repetitive code with variables? [closed]

I'm trying to eliminate a lot of repetitive code in my Python and this function repeats itself in various forms. Is there not an easier way I can just pass a variable (using a list or similar) to the ...
4
votes
4answers
210 views

Should exceptions be raised higher up or lower down or both? [closed]

When calling some function in a Python application, the function often calls functions deeper down which again call functions deeper down, etc. It is easy to unknowlingly pass a bad value to the ...
2
votes
2answers
307 views

Python OO problem

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it. As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some ...
6
votes
1answer
239 views

Why is universal newlines mode deprecated in Python?

I just noticed that the universal newline feature of file operations seems to be on its way out. The documentation for Python 3.5 open's mode parameter indicates that it's deprecated: 'U' ...
2
votes
1answer
131 views

Type checking as opposed to multiple functions?

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know): public String flip(String text) { String result = ""; for (int i = ...
0
votes
1answer
89 views

How to Handle Duplicate Web Requests on Server

I'm running a Twisted web server and trying to figure out a good way to handle retries from the client. In normal operation the client makes a request of the server with some JSON data and receives a ...
5
votes
1answer
111 views

How to organize my module

This is currently the way I'm organizing it. myProject/ README subproject1/ __init__.py classA.py classB.py driver.py basicTest.py Driver imports ...
2
votes
2answers
217 views

Exception handling in Python - Am I doing this wrong (and why?)

I've read many questions and articles on exception handling in Python (and in general), but I still think that it's the most confusing thing ever. I ended up doing something like this: # error class ...
1
vote
1answer
67 views

I am able to use PUT to act like GET when using my REST API

I wrote a simple REST API (just learning) using Go and I am calling it using python requests. I have two methods, "update_x", which updates the value of the resource, "get_x_times_n" which gets the ...
1
vote
2answers
105 views

How to achieve inheritance when using just modules and vanilla functions in Python?

Python doesn't enforce any paradigm. It gives you freedom. Python provides encapsulation at module level. If I have a module A and have a module B with same interface. How do I sort of inherit B ...
1
vote
0answers
65 views

How to re-use a very small Python utility across different applications?

I have a very small utility that has proven itself very handy across a multitude of applications. The utility itself is less than 50 lines of code and is its own Python module, and a number of ...
1
vote
1answer
181 views

Hard time understanding structure of web apps w JS frameworks? [closed]

I'm having a difficult time understanding how web applications work. Please help me understand without giving me google definitions. Baby steps/words would be appreciated =) From what I understand so ...
3
votes
3answers
75 views

Premature memoization on obvious cases

In the code below there will be several instances of MainClass. class MainClass(object): def f(self, x): # expensive operations. # ... return 'something' Each instance ...
0
votes
2answers
123 views

A Python program that illustrates the tree-like structure of recursion [closed]

I want to write a program in Python that illustrates the tree-like feature of recursion, using the example of "fibo(n)" function. I wonder how to modify the following program: def fibo(n): if ...
1
vote
4answers
112 views

What is the Pythonic convention for working with an object similar to an existing variable? [closed]

I often have an object, like a list or a dictionary, that I want a variation on. I might want a list with each element changed somehow (perhaps with a list comprehension, or could be a complicated ...
37
votes
6answers
3k views

Should I create a class if my function is complex and has a lot of variables?

This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in ...
0
votes
0answers
15 views

How do I write a loader library for a Python-based KIO slave

I'm trying my hand at writing a KIO slave and chose python because, well, that's what I know. There's even a decent example here Then here it tells you that for Python you need a C loader library ...
2
votes
0answers
71 views

Reducing conditional code makes assessing coverage or debugging difficult

Working in Python, I've written a few utilities to assert pre-conditions and to re-interpret exceptions, and have found my code doesn't have so many branches. I've also tried to move other code path ...
4
votes
2answers
139 views

How to handle singular and plural parameters for functions

Often when I'm writing a set of utility functions, I find myself wanting a function that applies some logic to multiple items, as well as a function that does the same with just a single item. There ...
5
votes
4answers
201 views

Shell commands in bash or python? How much encapsulation is too much?

I'm thinking about how to decide whether it's better to encapsulate my work behind well-named function names, or to expose it - which will help developers understand what's going on more quickly? Is ...
1
vote
1answer
99 views

How do I refactor a loop that does aggregation while doing it's regular job ?

What am I suppose to do I am working on an API, where I get a list of objects from the output of an SQL query using an ORM. Now this API call needs to send following information down to the client. ...
0
votes
2answers
119 views

Why does presence of value evaluate as “Truthy” in Python? [e.i. Types evaluate True in conditionals]

Example: if float: print('float is true?') returns: Why do all objects other than ones explicitly defined as False or "False" evaluate truthfully?
1
vote
2answers
125 views

writing a controller file in Python

I need some advice on my idea to write a controller file in Python, and not like the C in MVC type controller but a more simpler idea. Just a Python script that controls the operation of some other ...
2
votes
2answers
230 views

Is it OK to use (WTF) forms to validate against stuff from DB?

Form classes are intended (IMO) for submitted data against rules. For example: are passwords equal, is end date later than start date. submitted data--->|Form| Is it okay for Form classes to ...
0
votes
2answers
335 views

How are complex programs made? [duplicate]

I know a few languages, and can program in them. How do multiple languages blend together in a single program? For ex. https://github.com/facebook/watchman. This uses C, PHP, Python, Javascript, etc. ...
0
votes
2answers
197 views

Ensuring program output is always the same [closed]

I am working on a +10k LOC program, and I need to ensure its output is always the same for given input. The program consists of dozens of modules and classes, inherited by a MainClass. (The examples ...
8
votes
1answer
186 views

Is **kwargs an antipattern?

We have a lot of code in our internal codebase that calls our libraries internally - these libraries often have a lot of arguments (think matplotlib) and our code is often doing only a specific task ...
0
votes
0answers
58 views

Reusing models and classes with blueprints with flask

Designing a content management system using Python/Flask. I have a blueprint module called pages [app/pages/..]. Pages contains the title, description and content. It can be queried using ...
0
votes
0answers
14 views

How much additional specialised functionality should I bundle with my main flask application?

I am building a custom content management system (i.e. html, some js) for a project using Flask/Python. As things have progressed, I have seen that I need to add: Some AJAX for interactivity. ...
1
vote
2answers
112 views

Is it considered good practice to use mutables this way? [closed]

Lately I came across a way of using mutables which I had not thought of before. Let's say you have a list comprehension which calls a method, and you put the result in a list like the following ...