Found 27104 Articles for Server Side Programming

How to destroy an object in Python?

Sarika Singh
Updated on 18-Aug-2022 12:39:40

18K+ Views

When an object is deleted or destroyed, a destructor is invoked. Before terminating an object, cleanup tasks like closing database connections or filehandles are completed using the destructor. The garbage collector in Python manages memory automatically. for instance, when an object is no longer relevant, it clears the memory. In Python, the destructor is entirely automatic and never called manually. In the following two scenarios, the destructor is called − When an object is no longer relevant or it goes out of scope The object's reference counter reaches zero. Using the __del__() method In Python, a destructor is ... Read More

How do we access class attributes using dot operator in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 12:30:35

521 Views

A class attribute is an attribute of the class rather than an attribute of an instance of the class.In the code below class_var is a class attribute, and i_var is an instance attribute: All instances of the class have access to class_var, which can also be accessed as a property of the class itself −Exampleclass MyClass (object):     class_var = 2     def __init__(self, i_var):         self.i_var = i_var foo = MyClass(3) baz = MyClass(4) print (foo.class_var, foo.i_var) print (baz.class_var, baz.i_var)OutputThis gives the output(2, 3) (2, 4)

C++ 'a.out' not recognised as a command

Pythonista
Updated on 10-Feb-2020 10:47:17

4K+ Views

Having entered following command from linux terminal −$ g++ helloworld.cppThe a.out file should be created in the current working directory if the compilation is successful. Check if a.out is created.To execute enter following from command line −$ ./a.outIn most cases, output of your source program is displayed. However, as in your case, error message indicating a.out is not executable is appearing. See the properties of a.out and make it executable (if not already) by following command −$ chmod +x a.outYou may require sudo privilege for this. In all probability this should work. all the bestRead More

Python tuples are immutable then how we can add values to them?

Malhar Lathkar
Updated on 20-Feb-2020 11:29:08

169 Views

Python tuple is an immutable object. Hence any operation that tries to modify it (like append/insert) is not allowed. However, following workaround can be used.First, convert tuple to list by built-in function list(). You can always append as well as insert an item to list object. Then use another built-in function tuple() to convert this list object back to tuple.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> L1=list(T1) >>> L1 [10, 50, 20, 9, 40, 25, 60, 30, 1, 56] >>> L1.append(100) >>> L1.insert(4,45) >>> T1=tuple(L1) >>> T1 (10, 50, 20, 9, 45, 40, 25, 60, 30, 1, 56, 100)

How to append elements in Python tuple?

Sarika Singh
Updated on 22-Aug-2023 01:47:41

120K+ Views

Tuples in Python are immutable, meaning that once they are created, their contents cannot be changed. However, there are situations when we want to change the existing tuple, in which case we must make a new tuple using only the changed elements from the original tuple. Following is the example of the tuple − s = (4, 5, 6) print(s) print(type(s)) Following is the output of the above code − (4, 5, 6) Tuple is immutable, although you can use the + operator to concatenate several tuples. The old object is still present at this point, and ... Read More

How can I do Python Tuple Slicing?

Malhar Lathkar
Updated on 20-Feb-2020 11:27:57

299 Views

Slicing operator can be used with any sequence data type, including Tuple. Slicing means separating a part of a sequence, here a tuple. The symbol used for slicing is ‘:’. The operator requires two operands. First operand is the index of starting element of slice, and second is index of last element in slice+1. Resultant slice is also a tuple.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[2:4] (20, 9)Both operands are optional. If first operand is missing, slice starts from beginning. If second operand is missing, slice goes upto end.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[6:] (60, 30, 1, 56) >>> T1[:4] (10, 50, 20, 9)

How to pop-up the first element from a Python tuple?

Pythonic
Updated on 20-Feb-2020 11:27:15

3K+ Views

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

What is the difference between __str__ and __repr__ in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

137 Views

The built-in functions repr() and str() respectively call object.__repr__(self) and object.__str__(self) methods. First function computes official representation of the object, while second returns informal representation of the object. Result of both functions is same for integer object. >>> x = 1 >>> repr(x) '1' >>> str(x) '1' However, it is not the case for string object. >>> x = "Hello" >>> repr(x) "'Hello'" >>> str(x) 'Hello' Return value of repr() of a string object can be evaluated by eval() function and results in valid string object. However, result of str() can not be evaluated. ... Read More

How do I check whether a file exists using Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

517 Views

Presence of a certain file in the computer can be verified by two ways using Python code. One way is using isfile() function of os.path module. The function returns true if file at specified path exists, otherwise it returns false. >>> import os >>> os.path.isfile("d:\Package1\package1\fibo.py") True >>> os.path.isfile("d:/Package1/package1/fibo.py") True >>> os.path.isfile("d:onexisting.txt") Note that to use backslash in path, two backslashes have to be used to escape out of Python string. Other way is to catch IOError exception that is raised when open() function has string argument corresponding to non-existing file. try: fo = open("d:onexisting.txt", ... Read More

How to remove index list from another list in python?

Vikram Chiluka
Updated on 19-Sep-2022 11:03:49

2K+ Views

In this article, we will show you how to remove the index list elements from the original list using python. Now we see 2 methods to accomplish this task − Using pop() method Using del keyword Assume we have taken a list containing some elements. We will remove the index list elements from the main list using different methods as specified above. Note We must sort the indices list in descending order because removing elements from the beginning will change the indices of other elements, and removing another element will result in an incorrect result due to misplaced ... Read More

Advertisements