overwrite

Python method that returns instance of class or subclass while keeping subclass attributes

Python method that returns instance of class or subclass while keeping subclass attributes Question: I’m writing a Python class A with a method square() that returns a new instance of that class with its first attribute squared. For example: class A: def __init__(self, x): self.x = x def square(self): return self.__class__(self.x**2) I would like to …

Total answers: 2

python: multiple print lines to be overwritten

python: multiple print lines to be overwritten Question: Sorry for the noob question, I checked out other similar questions on the website but couldn’t find one that solves my problem. I’m very new to python so forgive me. I’m trying to make a bot which shows results of my trading account. First step I would …

Total answers: 3

Python for loop overwriting

Python for loop overwriting Question: Here is my code in Python 3: firstNode =[134, 135] USAdetail =[‘134.250.7.8’, ‘1.39.35.138’, ‘100.43.90.10’,’101.43.90.10′, ‘101.43.90.11’] for each in USAdetail: if each.split(‘.’)[0] in firstNode: print (“successful”) I thought it should print out “successful” since there is a “134” in USAdetail. However, it seems like it is overwriting and the last element …

Total answers: 1

How to 'update' or 'overwrite' a python list

How to 'update' or 'overwrite' a python list Question: aList = [123, ‘xyz’, ‘zara’, ‘abc’] aList.append(2014) print aList which produces o/p [123, ‘xyz’, ‘zara’, ‘abc’, 2014] What should be done to overwrite/update this list. I want the o/p to be [2014, ‘xyz’, ‘zara’, ‘abc’] Asked By: pyLearner || Source Answers: You may try this alist[0] …

Total answers: 6

Python: How to force overwriting of files when using setup.py install (distutil)

Python: How to force overwriting of files when using setup.py install (distutil) Question: I am using distutil to install my python code using python setup.py install I run into problems when I want to install an older branch of my code over a new one: setup.py install won’t overwrite older files. A work around is …

Total answers: 3

Is it possible to access original function which has been overwritten in python

Is it possible to access original function which has been overwritten in python Question: I was asked to add some feature to the code originally written by other guys. There is a python code defines a function which overwrites the build in open function def open(xxx): … I would like to access original open function …

Total answers: 3

Python, Overriding an inherited class method

Python, Overriding an inherited class method Question: I have two classes, Field and Background. They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): field = [0,0,0] return field class Background( Field ): def __init__( …

Total answers: 5

How to overwrite a folder if it already exists when creating it with makedirs?

How to overwrite a folder if it already exists when creating it with makedirs? Question: The following code allows me to create a directory if it does not already exist. dir = ‘path_to_my_folder’ if not os.path.exists(dir): os.makedirs(dir) The folder will be used by a program to write text files into that folder. But I want …

Total answers: 6

Python – Move and overwrite files and folders

Python – Move and overwrite files and folders Question: I have a directory, ‘Dst Directory’, which has files and folders in it and I have ‘src Directory’ which also has files and folders in it. What I want to do is move the contents of ‘src Directory’ to ‘Dst Directory’ and overwrite anyfiles that exist …

Total answers: 7

Read and overwrite a file in Python

Read and overwrite a file in Python Question: Currently I’m using this: f = open(filename, ‘r+’) text = f.read() text = re.sub(‘foobar’, ‘bar’, text) f.seek(0) f.write(text) f.close() But the problem is that the old file is larger than the new file. So I end up with a new file that has a part of the …

Total answers: 7