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

497 questions
7262
votes
25 answers

What are metaclasses in Python?

What are metaclasses? What are they used for?
Bite code
  • 578,959
  • 113
  • 301
  • 329
1319
votes
18 answers

What is a mixin and why is it useful?

In Programming Python, Mark Lutz mentions the term mixin. I am from a C/C++/C# background and I have not heard the term before. What is a mixin? Reading between the lines of this example (which I have linked to because it is quite long), I am…
TarkaDaal
  • 18,798
  • 7
  • 34
  • 51
96
votes
7 answers

Class Based Views VS Function Based Views

I always use FBVs (Function Based Views) when creating a django app because it's very easy to handle. But most developers said that it's better to use CBVs (Class Based Views) and use only FBVs if it is complicated views that would be a pain to…
catherine
  • 22,492
  • 12
  • 61
  • 85
7
votes
2 answers

Python dataclass, one attribute referencing other

@dataclass class Stock: symbol: str price: float = get_price(symbol) Can a dataclass attribute access to the other one? In the above example, one can create a Stock by providing a symbol and the price. If price is not provided, it defaults…
Mukesh C
  • 93
  • 7
7
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
  • 157
  • 11
5
votes
3 answers

How do I convert a json file to a python class?

Consider this json file named h.json I want to convert this into a python dataclass. { "acc1":{ "email":"acc1@example.com", "password":"acc1", "name":"ACC1", "salary":1 }, "acc2":{ …
Kanishk
  • 258
  • 1
  • 3
  • 9
5
votes
1 answer

Python nested dataclasses ...is this valid?

Background I'm using dataclasses to create a nested data structure, that I use to represent a complex test output. Previously I'd been creating a hierarchy by creating multiple top-level dataclasses and then using composition: from dataclasses…
Richard
  • 3,024
  • 2
  • 17
  • 40
5
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
  • 6,052
  • 8
  • 61
  • 129
4
votes
3 answers

Python decorator in a class: missing required positional arguments:

class Something: def __init__(self, ...): ... def update(self): ... def add_update(self, func): def fct(*args, **kwargs): self.update() func(*args, **kwargs) return fct @add_update …
user15822443
4
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
  • 2,594
  • 4
  • 24
  • 36
4
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
  • 1,040
  • 10
  • 23
4
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
  • 111
  • 1
  • 9
3
votes
1 answer

Problems with version control for dictionaries inside a python class

I'm doing something wrong in the code below. I have a method (update_dictonary) that changes a value or values in a dictionary based on what is specificed in a tuple (new_points). Before I update the dictionary, I want to save that version in a list…
Henri
  • 1,077
  • 10
  • 24
3
votes
2 answers

Wrapping derived class method from base class

Imagine I have a base and a derived class like so: class A: def foo(self): pass class B(A): def foo(self): pass I wish to wrap calls foo calls made by instances of B. Modifying any part of B is not allowed (I don’t own…
xrisk
  • 3,790
  • 22
  • 45
3
votes
1 answer

how do i call the parent method if i have a function with the same name in the current class (if possible)

I was reading on method overriding and in other languages, it seems that to be completely overridden, the method has to have the same signature (parameters, return type... etc) so I was trying to check if that's how it worked with python and I tried…
brightstar2100
  • 117
  • 1
  • 8
1
2 3
33 34