FI-Info
FI-Info

Reputation: 705

How to name class variables

Is there a convention on how class variables should be named (as different from instance variables), or is there no real difference. For example:

import pymysql

class DB:
    NUM_CONNECTIONS = 0
    def __init__(self):
        self.conn = None
        self.cursor = None
    def __repr__(self):
        return 'Closed connection' if not self.conn else f'Open conection @ 0x{id(self):x}'
    def connect(self):
        if self.conn: return
        self.conn = pymysql.connect(host='127.0.0.1',user='root')
        self.cursor = self.conn.cursor()
        DB.NUM_CONNECTIONS += 1

Currently I usually do variables as CLASS_VARIABLES and instance_variables. What are some common patterns for this?

Upvotes: 6

Views: 7006

Answers (2)

sanyassh
sanyassh

Reputation: 8500

Lets check what pylint thinks about class variable names. Consider the following code example:

# top-level constants for comparison
lowercase_var = 0
UPPERCASE_VAR = 1
CamelCaseVar = 2

def func():
    # function local variables
    lowercase_var2 = 0
    UPPERCASE_VAR2 = 1
    CamelCaseVar2 = 2


class ClassName:
    # class variables
    lowercase_var3 = 0
    UPPERCASE_VAR3 = 1
    CamelCaseVar3 = 2

Output from pylint:

D:\tmp>pylint test.py
************* Module test
test.py:1:0: C0103: Constant name "lowercase_var" doesn't conform to UPPER_CASE naming style (invalid-name)
test.py:3:0: C0103: Constant name "CamelCaseVar" doesn't conform to UPPER_CASE naming style (invalid-name)
test.py:8:4: C0103: Variable name "UPPERCASE_VAR2" doesn't conform to snake_case naming style (invalid-name)
test.py:9:4: C0103: Variable name "CamelCaseVar2" doesn't conform to snake_case naming style (invalid-name)
test.py:7:4: W0612: Unused variable 'lowercase_var2' (unused-variable)
test.py:8:4: W0612: Unused variable 'UPPERCASE_VAR2' (unused-variable)
test.py:9:4: W0612: Unused variable 'CamelCaseVar2' (unused-variable)
test.py:12:0: R0903: Too few public methods (0/2) (too-few-public-methods)

------------------------------------------------------------------
Your code has been rated at 2.73/10 (previous run: 1.82/10, +0.91)

Observations:

  1. No warning about any of three class variable names. All three are perfectly valid.
  2. Uppercase names are usually used for top-level constants. One can use uppercase naming convention for class variables to make them look like a constant value. If you are modifying the variable like DB.NUM_CONNECTIONS += 1 it is better to choose another naming style.
  3. snake_case naming style is used for function local variable names and function names, CamelCase for class name only. Class variable is a variable, it is not a class (but belongs to a class). So looks like it is better to choose snake_case.

Two last observations are opinionated and you can disagree and make your own choice. The good news: you can choose any from the three options.

Upvotes: 4

alex067
alex067

Reputation: 3281

You should follow PEP8, but generally from my experience, companies / individual developers either stick to camel case or snake case, for typing conventions.

Camel case is type writing that capitalizes the second word of a variable name, IE: myPhoneBook, as opposed to snake case, which uses underscores to separate words in a variable name, IE: my_phone_book

It's a good idea to find one that works for you, and to stick with it.

Variable names that are all uppercase, are generally reserved for environment variables.

Other than that, do not use keywords as variable names. Often times, people will use "list" as a variable name, which is extremely dangerous, as "list" is a keyword that is provided natively by Python. Using keywords as variables means you are effectively replacing that keyword.

Upvotes: 0

Related Questions