methods

Importing methods from built-in class for use in custom class – Python

Importing methods from built-in class for use in custom class – Python Question: I created a class with extended methods for the built-in ‘str’ class, but I want the user to be able to use those normal built-in methods for strings as well as my extra methods. How would I do this without having to …

Total answers: 1

When I call two methods, one from the parent class and one from the child class, only the first call returns

When I call two methods, one from the parent class and one from the child class, only the first call returns Question: class Smarthphone(): def __init__(self, tamanho, interface): self.tamanho = tamanho self.interface = interface def print_test(self): return "testing parent class" class MP3Player(Smarthphone): def __init__(self, tamanho, interface, capacidade): super().__init__(tamanho, interface) self.capacidade = capacidade def print_MP3Player(self): return …

Total answers: 1

How to get the values of instances using classes?

How to get the values of instances using classes? Question: i’am new into classes in python. And i’am running into an error, so i wanted to calculate the distance between two coordinates. But i’am struggling to get the values x and y values from p1 and p2 or p2 and p3,… And to use theses …

Total answers: 1

Python object. How to pass a value to one specific method

Python object. How to pass a value to one specific method Question: I have a Python object that represents a camera dolly that moves along a track. The class can store and report its position along the track. I have three methods: MoveBy – which gets the distance from another object (Movie.yIncrement) MoveTo – which …

Total answers: 1

Changing __setattr__ behaviour via decorator

Changing __setattr__ behaviour via decorator Question: How can apply a decorator to __setattr__ method? I’ve tried this way but print inside of wrapper doesn’t work. (I know we can change setattr via inheritance, but need to do it via decorator / other way). Thanks in advance. from functools import wraps class A(): pass x = …

Total answers: 2

Strange python closure variable access

Strange python closure variable access Question: While trying to implement a decorator with a closure I ran into a somewhat strange behavior, where a variable can be read, but if try to assign it later, it becomes undefined even before the assignment. def run_once(fn): to_run = True def decorated(*args, **kwargs): if to_run: print(to_run) #this access …

Total answers: 1

How to reference class variable name in class method definition in python?

How to reference class variable name in class method definition in python? Question: I’m trying to use the variable name of my class inside of a method in my class, but I have no idea how to do it. To better explain here is an example. class Pet(): def __init__(self, name, species): self.name = name …

Total answers: 1

Series.Items() Method Returns Zip Instead of Expected Output (Pandas)

Series.Items() Method Returns Zip Instead of Expected Output (Pandas) Question: I am attempting to access the index of a series returned by the series.items() method in Pandas. I was able to successfully do so when the series I generated was the result of a groupby: df = pd.DataFrame( { ‘species’: [‘dog’, ‘cat’, ‘horse’, ‘dog’, ‘cat’, …

Total answers: 1