class

How do I create a class in Python that contains a udp socket?

How do I create a class in Python that contains a udp socket? Question: I created a class that acts as a UDP server on port 1500 import socket class Udp_Server(): def __int__(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.bind(("", 1500)) def recv_udp(self): data, addr = self.sock.recvfrom(1024) return data, addr In main, I call the recv_udp function …

Total answers: 1

Beginner Classes – Calling a variable name in a function and if I used a try/except properly

Beginner Classes – Calling a variable name in a function and if I used a try/except properly Question: I just started learning Python/coding in general. I tried to find an answer to this but couldn’t. So I created this code for a Class "Vehicle" in my lab: class Vehicle(object): def __init__(self, maxspeed, mileage, color="white"): self.maxspeed=maxspeed; …

Total answers: 3

Particle Trajectory, to learn classes and data structures

Particle Trajectory, to learn classes and data structures Question: So the task is supposed to be done in multiply steps. So we are given a class Particles, that we have to define. It is supposed to have multiply methods, and here is where i meet my first struggle. Create a class called Particle that has …

Total answers: 1

What is the right way to implement addition using class in Python and avoid TypeError with missing positional argument?

What is the right way to implement addition using class in Python and avoid TypeError with missing positional argument? Question: class Add: def __init__(self,a,b): self.a = a self.b = b def add(a,b): return self.a + self.b obj = Add(3,4) print(obj.add()) Error message: print(obj.add()) ^^^^^^^^^ TypeError: Add.add() missing 1 required positional argument: ‘b’ Asked By: Madhu …

Total answers: 2

How can i add buttons afterly in Discord.py?

How can i add buttons afterly in Discord.py? Question: I am making a Discord bot for an order. And I need to make a role giver button list and add buttons to the first message after with a command I trying this for 2 days but I can’t add buttons only edit the first message …

Total answers: 1

What is the need to define a function to return a class's attribute?

What is the need to define a function to return a class's attribute? Question: I came across some code like this class Student(Person): def __init__(self, age): self.age = age def get_age(self): return self.age Could you please explain the purpose of the function get_age()? If we want to know a student’s age, can’t we simply do …

Total answers: 1

How to treat a Python object as the value of an attribute of that object?

How to treat a Python object as the value of an attribute of that object? Question: I’m currently writing a Python project in which I need to pass around a custom class representation of an 8-bit byte. I can access an individual byte with __getitem__() (for example, the first byte inmy_byte = Byte("00000001") would be …

Total answers: 1

implement class method based on instance variable (OOP)

implement class method based on instance variable (OOP) Question: i am new to oop. i want to create objects that have a method which depends on the instance variables. class cases: def __init__(self, a): self.a = a def f1(self, x): return x def f2(self, x): return 100*x if self.a < 3: self.x = f1 else: …

Total answers: 1