Questions tagged [python-class]

For questions that relate to the use of classes as a language feature in Python.

This tag is for questions that are about the use of classes in Python. Some aspect of the question should include a non-trivial inquiry related to the use of classes as a language feature. Do not use this tag just because the code happens to define or use a class.


From the official documentation on Python classes:

Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

[...] Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.

In C++ terminology, normally class members (including the data members) are public (except see below Private Variables), and all member functions are virtual. As in Modula-3, there are no shorthands for referencing the object’s members from its methods: the method function is declared with an explicit first argument representing the object, which is provided implicitly by the call. As in Smalltalk, classes themselves are objects. This provides semantics for importing and renaming. Unlike C++ and Modula-3, built-in types can be used as base classes for extension by the user. Also, like in C++, most built-in operators with special syntax (arithmetic operators, subscripting etc.) can be redefined for class instances.

See also: Tag wiki for OOP

199 questions
6178
votes
21 answers

What are metaclasses in Python?

In Python, what are metaclasses and what do we use them for?
e-satis
  • 515,820
  • 103
  • 283
  • 322
3
votes
1 answer

Adding arbitrary extra attribute to numpy.ndarray

With a 'normal' Python class I'm used to being able to arbitrarily add extra attributes. For example, I could do the following: # Create a class class MyClass: pass # Create an object of this class my_object = MyClass() # Add any attribute I…
Lara
  • 1,662
  • 4
  • 19
  • 34
3
votes
3 answers

Python (pandas): Using decorators using pandas API

I'm quite new to decorators and classes in general on Python, but have a question if there is a better way to decorate pandas objects. An an example, I have written the following to create two methods -- lisa and wil: import numpy as np import…
Mathew Carroll
  • 336
  • 2
  • 8
3
votes
1 answer

python: is it legal to pass self to the nested function inside the class method?

class A: def __init__(self): self.name = None self.a = 10 self.b = 20 self.c = 30 def func1(self, param1, param2): def inner_func1(self, param1, param2): print(self, self.a, self.b) …
Mark
  • 4,824
  • 5
  • 38
  • 85
2
votes
1 answer

Accessing entry field value of one tkinter class in another

I have just started learning tkinter and came across a problem.I have two tkinter classes. I am entering a value in an entry field of one tkinter class and trying to show it in the label in the other class. I have tried it many ways but not able to…
Anmol
  • 37
  • 3
2
votes
1 answer

VSCode PyLint Not detecting my Python DTO Class Members

Python Lint does not detect incorrect class members. It continues running my code, I have productName member below, not productNameTest. It should be sending an error. How can this be resolved ? Currently using VS Code. Product…
2
votes
1 answer

Why does this parent class setter call use type(self) rather than self?

Python @property inheritance the right way explains how to call the parent setter. class Number: def __init__(self): self._value = None @property def value(self): assert self._value is not None return…
mon
  • 8,766
  • 4
  • 52
  • 87
2
votes
2 answers

Making A Class Constant Who's Type Is The Class In Which It Resides

I have a Python class with special values, "EMPTY" and "UNIVERSE": class RealSet: """Continuous open, half-open, and closed regions and discreet values of the Reals""" # implementation placeholder def __init__(self, intervals, *,…
Brent
  • 3,489
  • 3
  • 22
  • 55
2
votes
1 answer

Why doesn't python throw an exception when you set an attribute of a class that doesn't exist

I was trying to debug my code forever, and it turns out that this was the cause of my error, making it so hard to find. A simple example to demonstrate what I'm talking about: class Test(): def __init__(self): self.a = 0 x = Test() x.b =…
fooiey
  • 549
  • 1
  • 12
2
votes
1 answer

Set class attribute by awaiting a coroutine in Python

I have a class that has an attribute that holds a Redis connection as seen below: import redis class RedisService: db = redis.Redis(host=RedisConfig.REDIS_HOST, port=RedisConfig.REDIS_PORT) @staticmethod def exists(key): return…
iedmrc
  • 662
  • 2
  • 8
  • 18
2
votes
2 answers

Group related constants in Python

I'm looking for a pythonic way to define multiple related constants in a single file to be used in multiple modules. I came up with multiple options, but all of them have downsides. Approach 1 - simple global constants # file…
EvilTosha
  • 107
  • 9
2
votes
0 answers

Nested classes/functions that call one another and must inherit their parents' properties

I have a rather odd scenario, and I just can't wrap my head around the proper solution. I'm working on a project that has multiple functions to perform certain operations: These functions are interconnected and need to be able to call each other…
2
votes
1 answer

Create multiple instances of pybullet client within a python class

I am using pybullet in a python class. I import it as import pybullet as p. When I have several instances of the class using pybullet, is the class p the same for each instance or is the "variable" p unique for each instance? foo.py import pybullet…
FR_MPI
  • 59
  • 5
1
vote
2 answers

Best practice for providing optional functions for a class in Python

Currently I am writing a Python program with a plugin system. To develop a new plugin a new class must be created and inherit from a base plugin class. Now it should be possible to add optional functions via mixins. Some mixins provide new functions…
DasLukas
  • 139
  • 8
1
vote
1 answer

Graph Class: TypeError: __init__() takes 1 positional argument but 3 were given

class _Edges(defaultdict): def __missing__(self, vertex): self.setdefault(vertex, True) def __delitem__(self, dst): self[dst] = False def del_vertex(self, dst): super().__delitem__(dst) class…
1
2 3
13 14