Difference between 'instance', 'class', and 'static' method visually explained

4 months ago by bterwijn to c/python

tunetardis 2 points 4 months ago

Coming from C++, I initially thought it aggravating that you have to go self. everywhere when working with classes in Python. But I've conceded that it improves code-legibility, and now I'm going this-> everywhere when I'm back in C++.

path: 0 24522367, hotness: undefined, score: 2, children: 4
bterwijn 0 points 4 months ago

Another common naming convention in C++ is using an underscore as prefix for member variables: int _memberVariable{123};

path: 0 24522367 24522478, hotness: undefined, score: 0, children: 3
tunetardis 1 point 4 months ago

My personal convention in C++ was to use an m prefix on member variables and a g for class variables.

I guess in Python, I think of _ as equivalent to protected scope in C++ and __ (double underscore) as equivalent to private. Not a perfect analogy, but it works for me.

path: 0 24522367 24522478 24522924, hotness: undefined, score: 1, children: 2
aion 2 points 4 months ago

Just don't use double underscores in C++, identifiers containing double underscores are reserved for the compiler/linker/standard library.

path: 0 24522367 24522478 24522924 24523186, hotness: undefined, score: 2, children: 1
bterwijn 1 point 4 months ago

Same thing in Python, double underscore prefix triggers "name mangling". There are issues that can pop up when using it if not aware.

path: 0 24522367 24522478 24522924 24523186 24536297, hotness: undefined, score: 1, children: 0
python
python

@programming.dev

login for more options
8076
745
295

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events
Past

November 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
  • Pythรถrhead: a Python library for interacting with Lemmy
  • Plemmy: a Python package for accessing the Lemmy API
  • pylemmy pylemmy enables simple access to Lemmy's API with Python
  • mastodon.py, a Python wrapper for the Mastodon API
Feeds

go to feed...