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
1 answer

TypeError: draw_shape() missing 1 required positional argument: 'self'

I'm trying to code a simple game of pong as a way to learn pygame as i'm not that experienced in coding. I've only recently started to use classes and i guess i don't quite understand how to use init properly As whenever i run the code: class…
Bonzo 998
  • 3
  • 2
-2
votes
2 answers

passing self with other argument on function call python

I have my code below. I want to call the "speak" function with two arguments inside the main() class. When I call speak it says that self its not defined I'm new to POO. class main(): def blueon(self): print("teste") def…
ATEK0
  • 82
  • 9
-2
votes
1 answer

python and DRF :__init__() missing 2 required positional arguments: 'code' and 'username'

I try to use a simple inheritence in DRF project. so I have got two classes. I try to access a class attrs in my second class. but every thing I get is None value for both.when I try to use initializer, I get an error, which is my title. class…
user15446178
-2
votes
1 answer

Able to change class attributes without using a classmethod and staticmethod. Then what is the use of it?

In the below code snippet, no classmethod or staticmethod is used but class property can be altered just by defining normal methods inside a class, then what's the point? Am I missing something? Can anyone please explain this behavior? class test: …
Kazee
  • 31
  • 3
-2
votes
1 answer

Accessing class variables outside the class in python

I have a tkinter class. I want to access the value of entry field outside the class. I have tried doing it by creating a function but it is printing the address and not the value. Here is my code class first: def __init__(self, root): …
Anmol
  • 57
  • 1
  • 7
-2
votes
4 answers

Efficient class iteration in Python

I have a python class called StudentGrades, that is something like class StudentGrades: def __init__(self, scores): self.scores = scores def average(self): return sum(self.scores) / len(self.scores) def…
Rodrigo A
  • 657
  • 7
  • 23
-2
votes
1 answer

Is there a way to convert a python class into bytes?

I want to send a python class from one computer to the other. In order to send data, it must be in byte form. I know I can do this with dictionaries, but I was wondering if there is a way to convert a class into bytes? I'd prefer to use something…
CircuitSacul
  • 1,594
  • 12
  • 32
-2
votes
1 answer

How to add optional arguments in a python class?

I am trying to call different functions based on the value for rb_selection, calling func1 if rb_selection value is 0 and calling func2 if rb_selection value is 1. Both functions take a different set of arguments. I do not need folder…
shubhasreepv
  • 51
  • 1
  • 6
-3
votes
1 answer

Python OOP datatype

Why does it say 'Die eingegebenen Daten haben den falschen Datentyp!' when the datatypes are actually right? Doesn't even work with just matrNr... Although I checked my input of matrNr to be an int?! class Student: def __init__(self): …
lindafl0w
  • 3
  • 2
-3
votes
2 answers

Does python have super().__init__().area() this kind of method?

class Square(): def __init__(self,side): self.side = side def area(self): return self.side * self.side class cube(Square): def area(self): return super().__init__().area() * 6 def volume(self): …
-3
votes
2 answers

what is ` __init__()` in a python class

I'm a novice in python programming. I've been jumping from one resource to another to really grasp a low-level understanding of what __init__() is which normally appears at the beginning of a python class. When do we use it? What's a practical…
-3
votes
2 answers

python class problem with player movement

Im learning python i created code for player movement, it works when it isnt in class, but i need it in class. when i press W or S the player moves only once by vel = 5 and then it comes back to its original coordinates. How to fix it ? right =…
360yogi
  • 3
  • 2
-3
votes
2 answers

How to access a classes attribute variable in other method

I am learning python3 and I want to know that how I can class testit: def assigna(): a = "Hi" def geta(self): print(self.a) test = testit print(test.geta()) How can I access a inside of geta as suggested in comments I…
For This
  • 153
  • 1
  • 2
  • 8
-4
votes
1 answer

How to call a method of a class in python?

With below code I am trying to find the oldest of the 3 cats. When I try to call the method 'cat_age' of class 'Cat' in the print statement it gives the below error: NameError: name 'cat_age' is not defined Below is the code: class Cat: def…
-4
votes
2 answers

Changing the value of a class attribute using an instance

I am learning about classes and objects in python. I encountered a problem when I tried to create a class attribute whose value can be changed using an instance of that class. Lets assume create a class Student for students who go to the same…
Amani
  • 57
  • 4
1 2 3
33
34