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
1
vote
0 answers

python3 weakref() class iterator lost between functions

I have implemented an iterable class in python3.8 following this post implementing the weakref() function in order to be able to delete elements of the class, as explained in the post's accepted answer. The problem is that if I populate the iterable…
cccnrc
  • 1,154
  • 8
  • 21
1
vote
2 answers

Best possible method of error handling of clock class?

I want the code to stop working and return that the input time(hour) etc. is invalid as it is not between 1-24. However due to str statement of the class the invalid time still prints out. Is there anyway to show error without printing out the…
Zuzu
  • 69
  • 4
1
vote
2 answers

Python - error in calling a function from a class

I am trying to use a code that has been run successfully before by my colleagues. But, when I try to replicate the situation I get an error. Here is the situation: Class and function defined as: Class X: def f1(self, params) ... def…
SGotham
  • 77
  • 4
1
vote
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
  • 1,409
  • 1
  • 7
  • 25
1
vote
0 answers

Class heritage in Python with dynamically generated method

I've created a module that includes 3 main things: A main class with a __new__ method and child classes that inherit this one; A make_method decorator that can associate a method to an existing class; A generic class. The main class has a __new__…
1
vote
2 answers

Why schedule and requests library don't work with this Class in Python?

I'm a very beginner. I have this Class where I want to get some data from a website once every day (here every seconds because I was testing it) at a certain hour. I want to use schedule module and I can't figure what is the problem. I use Pycharm…
1
vote
1 answer

Assigning dictionary to a class object

What is the difference between the two class definitions below, class my_dict1(dict): def __init__(self, data): self = data.copy() self.N = sum(self.values) The above code results in AttributeError: 'dict' object has no…
phoxd
  • 1,278
  • 3
  • 10
  • 24
1
vote
1 answer

How to properly inherit class method

I have a database connection class that creates a connection pool. Now as the application grows and I'm adding different types of database writers, I want to move database connections to a separate class and inherit from it. So far I have…
G.M
  • 613
  • 1
  • 15
  • 34
1
vote
1 answer

Python's asyncio.Event() across different classes

I'm writing a Python program to interact with a device based on a CAN Bus. I'm using the python-can module successfully for this purpose. I'm also using asyncio to react to asynchronous events. I have written a "CanBusManager" class that is used by…
1
vote
1 answer

Data created with class failes to be displayed with flask app into html

I'm tring to make a flask application for a tool that I've created in my internship. I have a class Activities that can generate a pandas dataframe, and I wish to display it into a table in an HTML using flask. I tested a simple example that works…
Kiwi
  • 46
  • 7
1
vote
1 answer

I want to call parent class method which is overridden in child class through child class object in Python

class abc(): def xyz(self): print("Class abc") class foo(abc): def xyz(self): print("class foo") x = foo() I want to call xyz() of the parent class, something like; x.super().xyz()
Syed Fasih
  • 23
  • 6
1
vote
1 answer

Multiple Inheritance in Python Confusion

I have the following piece of code implementing multiple inheritance. I expect the call super(base2,self).__init__() to print --> "Printing from base2". But the program prints nothing; neither it throws error. class base1: def __init__(self): …
user3103957
  • 492
  • 2
  • 10
1
vote
4 answers

Function in Class error: TypeError: function() missing 1 required positional argument:

I'm relatively new in programming with Python. This code was working perfectly, until I tried turning it into a class. I'm making a class for my sudoku solver code, to practice classes and dabbling my toes in object oriented programming. So I have…
1
vote
1 answer

can I bypass passing self object to a python class method?

I am working with google classroom api and one of the params of a function takes a callback. def callback(request_id, response, exception): if exception is not None: print 'Error adding user "{0}" to the course course: {1}'.format( …
sakib11
  • 376
  • 3
  • 13
1
vote
2 answers

Function Chaining in Python, ignore rest of chain if a segment returns False

The title pretty much explains the problem. I don't know if there's a practical solution to this or if I'm being too picky over the behavior of my code. This article was hinting in the right direction, but I never got any code to…
1 2
3
13 14