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
2
votes
2 answers

Keyword arguments in python class inheritance

Hello I am recently looking about the metaclass in Python. I learned about we can attach a metaclass to a class in this way: class Metaclass(type): ... class MyClass(metaclass=Metaclass): ... The first question I wonder what's the…
WW00WW
  • 417
  • 4
  • 15
2
votes
0 answers

Python: Operation between integer and custom class

I created a class called Rational - which represents a rational number - with a method that allows it to be added to another Rational object, but am having a hard time figuring out a way to allow it to be added to an integer object through a method…
2
votes
0 answers

Is there a way to trace all methods called on an instance of a class, including dunder methods?

I'm looking for a means to trace all calls on an instance of a class, including calls to dunder methods like __iter__, __getattribute__ etc. I've tried using sys.setprofile, the autologging package, but failed : it doesn't seem to be able to trace…
Paje
  • 31
  • 4
2
votes
2 answers

Is there a way to disable some function in python class so that it cannot be used except using it in inside its class?

for example i have myClassFile.py file with code as follow: class myClass: def first(self): return 'tea' def second(self): print(f'drink {self.first()}') then i have run.py file with code as follow: from myClassFile import…
Hanzcerb
  • 51
  • 6
2
votes
1 answer

how to properly initialize a child class of XGBRegressor?

I want to build a quantile regressor based on XGBRegressor, the scikit-learn wrapper class for XGBoost. I have the following two versions: the second version is simply trimmed from the first one, but it no longer works. I am wondering why I need to…
Li-Pin Juan
  • 1,156
  • 1
  • 13
  • 22
2
votes
1 answer

How to access data in this model class?

I'm using package libmf to do parallel non-negative matrix factorization, i.e., X = WH. I use the method fit from the class MF. As mentioned in below description, the resulting matrices are stored in MF.model. def fit(self, X): """ factorize…
Akira
  • 2,594
  • 3
  • 20
  • 45
2
votes
0 answers

Creating a list with extra custom methods getting an error in python 3.8 but not 3.9

I am trying to make a class object that is essentially a list of dictionaries formatted by a given TypedDict. The example Attendings class can be seen below: from typing import TypedDict, List, get_args class Database(List): def…
2
votes
1 answer

calling a class below of another class

I'm working on a django project with rest_framework and I have a problem with serializers. Here is my code: class CategorySerializer(serializers.ModelSerializer): featured_product = ProductSerializer(read_only=True) class Meta: model…
2
votes
3 answers

Best way to handle imports used by only some Python classes

I'm making a Python package, inside a module, I have several python classes, but only one of them uses a specific package (tensorflow), which is installed using the extras_require option in setup.py file, since it's a heavy dependency and it's used…
Rodrigo A
  • 657
  • 7
  • 23
2
votes
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…
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
  • 57
  • 1
  • 7
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
  • 4,153
  • 4
  • 30
  • 63
2
votes
4 answers

Python dataclass: Check that a field is in list of possible values

I want a field in a Python dataclass to only have certain possible values. For instance, I want a Jewel dataclass, whose field material can only accept values "gold", "silver", and "platinum". I came up with this solution: @dataclass class Jewel: …
Enrico Gandini
  • 855
  • 5
  • 29
2
votes
1 answer

Enum class with type `type` as mixin

The docs state that it is possible to make an enumeration class whose members are exactly of a certain type by adding that type as a mixin to the enum class: While :class:IntEnum is part of the :mod:enum module, it would be very simple to implement…
Anakhand
  • 2,838
  • 1
  • 22
  • 50
2
votes
2 answers

Python — Init class reflectively

I am creating a commands system in Python. I have module vkcommands that has a class that processes commands from chat (this is a chat-bot), and inside it, I also have class VKCommand with attributes like name, usage, min_rank, etc. Then I have…
German Vekhorev
  • 339
  • 6
  • 16
1 2
3
33 34