Python | Declaring 'Class' and 'Instance' variables

How should one declare 'class' and 'instance' variables in Python ?

Well, in Python, assignment is declaration. You just need to assign the variables in the appropriate place.

If you intend to declare a 'class' variable, that is, a variable stored for the class and accessible through each instance, you code 'name = value' for that variable inside the class statement.

If you intend to declare a separate attribute for each instance, you code 'name = value' for that variable it in the '__init__' method.


Python | Socket Starter

I dont know why, but almost every programmer who learns a new language want's to program sockets right away. If you are one of these species, and you've just learnt python, here's how it can be done. Very simple really.

import sockets
session = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
session.connect((ipaddress, port))

thats it !

Of course you will need to understand more than that. So here's a URL for you.


C | C++ | Python | Control Rounding when converting Floats to Integers

I have faced this problem in the past, but had not thought of a solution yet. Was chatting with a friend lately, and he suggested a very simple idea.

The problem is that when converting Floats to Integers, they always round down. But you want them to round up sometimes, for whatever reason. You could do this :

Integer = Float + 0.5f

This will add .5 to the float number, and it will round down to either trunc (float) or trunc (float + 1).

Thus, 1.1 rounds to 1, and 1.7 to 2. You get the idea.


C++ | Be careful when writing assignment operators

If you are writing an assignment operator for a class, be very careful and ensure each of the following :

- that you have copied all the attributes
- that you have allocated new memory for each non-stack attribute (i.e. pointer variable) before copying it.

The second one is the one where most mistakes are made. One tends to copy the address pointed to by the source variable into the destination variable, which is perfectly legal but rarely intended. And then somewhere during runtime, the address in the source copy is freed and later, the address in the destination copy is accessed. And then guess what happens !


Python | limiting lines to 80 chars ? Are you crazy ? Is this COBOL or what ?

A Python Style Question : according to PEP-8 http://www.python.org/peps/pep-0008.html lines should have a max of 79 characters because "There are still many devices around that are limited to 80 character lines". My question is this, should i try and force myself to write code in this style?

Answers :

Follow it wherever possible, a better reason to follow that is that longer lines are harder to read.
I don't count mine at all, however I try to keep them short enough to read easily
That style guide is for code in the mainline python, that document doesn't suggest that *your* code should follow that guide
I do work on 80 char terminals at times and yeah, it's very annoying when lines wrap but even if I'm in X, I find long lines difficult to read. Also, short lines make interactive debugging easier.

Fundamentally, writing readable code is more about common sense than following style guides, they cannot account for every contingency, and, like all things, are imperfect.


C|C++|The correct way to include header files

The compiler searches for the files in different locations based on the the way they are included.
If included with < > , it searches in the predefined include path, and if included with " ", then it searches in the current directory.
Examples :

#include <>
#include "mymodule.h"


Python| get a string of all the lowercase letters ['a'..'z']

1. string.lowercase

chr(n) for n in range(ord('a'), ord('z')+1)