Found 10784 Articles for Python

How to get the return value from a function in a class in Python?

Rajendra Dharmkar
Updated on 09-Sep-2023 23:05:46

12K+ Views

The following code shows how to get return value from a function in a Python class.Exampleclass Score():     def __init__(self):         self.score = 0         self.num_enemies = 5         self.num_lives = 3     def setScore(self, num):         self.score = num     def getScore(self):          return self.score     def getEnemies(self):         return self.num_enemies     def getLives(self):         return self.num_lives         s = Score() s.setScore(9) print s.getScore() print s.getEnemies() print s.getLives()Output9 5 3

How to return an object from a function in Python?

Rajendra Dharmkar
Updated on 09-Sep-2023 23:08:13

13K+ Views

The return statement makes a Python function to exit and hand back a value to its caller. The objective of functions in general is to take in inputs and return something. A return statement, once executed, immediately halts execution of a function, even if it is not the last statement in the function.Functions that return values are sometimes called fruitful functions.Exampledef sum(a, b):      return a+b sum(5, 16)Output21Everything in Python, almost everything is an object. Lists, dictionaries, tuples are also Python objects. The code below shows a Python function that returns a Python object; a dictionary.Example# This function returns ... Read More

How do you compare Python objects with .NET objects?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

125 Views

By default, all .NET objects are reference types and their equality and hash code is determined by their memory address. Additionally, assigning a variable to an existing object just makes it point to that address in memory, so there is no costly copying occurring. It appears that this is true for python objects as well to certain extent.Properties of Python objects: All python objects havea unique identity (an integer, returned by id(x)); a type (returned by type(x))You cannot change the identity; You cannot change the type.Some objects allow you to change their content (without changing the identity or the type, that is).Some ... Read More

How to convert JSON data into a Python object?

Niharika Aitam
Updated on 15-May-2023 13:55:27

765 Views

JSON can be abbreviated as JavaScript Object Notation. Json means a script of a text file in a programming language to transfer and store the data. Json supported by the python programming language using a built-in package named json. The Json text is given in the quoted string format which contains in the key and value within the curly braces{}. This looks like a dictionary format in python programming language. For using this json package in the python programming language we have to import the json package in python script. In the Json package we have so ... Read More

How we can convert Python objects into JSON objects?

Niharika Aitam
Updated on 15-May-2023 13:53:04

792 Views

JSON can be abbreviated as JavaScript Object Notation. Json means a script of a text file in a programming language to transfer and store the data. Json supported by the python programming language using a built-in package named json. The Json text is given in the quoted string format which contains in the key and value within the curly braces{}. This looks like a dictionary format in python programming language. For using the json package in the python programming language we have to import the json package in python script. In the Json package we have ... Read More

How to find out if a Python object is a string?

Niharika Aitam
Updated on 15-May-2023 13:49:07

228 Views

We know that Python is an object oriented programming language. By using the oops concept we can make the code and functions work together in a better way. The OOPs in python help us to deal with the real time entities with inheritance, polymorphism, encapsulation etc. Group of objects is called a class. The class contains the blueprint, which takes as the reference to create the object. This contains attributes and methods in a logical entity. Let’s understand the usage of the class in object oriented programming language by a real time scenario. Consider a library. ... Read More

How to attach a C method to existing Python class?

Niharika Aitam
Updated on 15-May-2023 13:48:03

144 Views

We are currently coding of python C methods are being used. The base of all the libraries such as Numpy, Opencv, pytorch etc are built with the C and C++ i.e. these libraries internally call the Compiled C code where code will be executed in the machine and the result will be return in python wrapper. Why are we using the C methods in python? The reason why we are using the C method in python is the performance. The python performance will be decreased because of the dynamic typing. It has to reduce the type of operands to ... Read More

Do you think declarations within Python class are equivalent to those within __init__ method?

Rajendra Dharmkar
Updated on 16-Jun-2020 08:19:01

73 Views

Declarations anywhere in the class(other than in __init__) and declarations in the __init__method are not the same. The following code shows this to be true.Exampleimport sys class foo():     print 'within class'     def __init__(self):         print 'within init'     def do_smthng(self):         print 'do something' def main():     f=foo()     f.do_smthng()     return 0 if __name__ == '__main__':     sys.exit( main() )Outputwithin class within init do something

What's the best place for python classes in a Django project?

Niharika Aitam
Updated on 15-May-2023 13:46:30

201 Views

Django is one of the frameworks used along with python to create the web pages in an efficient manner. It is also called as the framework included with batteries why because by default the Django framework gives the admin interface and database interface like SQLite 3 etc. It also provides us the default readymade components namely user authentication handling like sign in, signup and sign out. It provides the management panel for our website, forms, uploading the files etc. Why are we using the Django framework? Django is known for its scalability and comprehensive documentation. This framework is used by ... Read More

How we can split Python class into multiple files?

Niharika Aitam
Updated on 15-May-2023 13:44:38

2K+ Views

OOPs is abbreviated as object oriented programming language. It helps us to deal with the real time entities with inheritance, polymorphism, encapsulation etc. By using the oops concept we can make the code and functions work together in a better way. Group of objects is called a class. The class contains the blueprint, which takes as the reference to create the object. This contains attributes and methods in a logical entity. Now let’s see why we have to split the code into multiple classes. If we create the entire code in the single class, it will be easy to write ... Read More

Advertisements