Programming Articles - Page 3251 of 3366

How to use Python object in C++?

Rajendra Dharmkar
Updated on 10-Feb-2020 10:49:28

871 Views

Here is an example in which a simple Python object is wrapped and embedded. We are using  .c for this, c++ has similar steps −class PyClass(object):     def __init__(self):         self.data = []     def add(self, val):         self.data.append(val)     def __str__(self):         return "Data: " + str(self.data) cdef public object createPyClass():     return PyClass() cdef public void addData(object p, int val):     p.add(val) cdef public char* printCls(object p):     return bytes(str(p), encoding = 'utf-8')We compile with cython pycls.pyx (use --cplus for c++) to generate ... Read More

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

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

15K+ 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?

Akshitha Mote
Updated on 23-Jun-2025 17:33:43

16K+ Views

In Python, we can return an object from a function, using the return keyword, just like any other variable. The statements after the return will not be executed. The return keyword cannot be used outside the function. If the function has a return statement without any expression, then the special value None is returned. In the following example, the function returned the sum of two numbers - def Sum(a, b): return a+b my_var1 = 23 my_var2 = 105 result_1 = Sum(my_var1, my_var2) # function call print(result_1) Following is an output of ... Read More

How do you compare Python objects with .NET objects?

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

207 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 we can compress large Python files?

Niharika Aitam
Updated on 28-Feb-2025 16:26:54

330 Views

We have to work with large size files in Python. If we use the large files, they occupy more space, and it isn't easy to handle the large files too. In such cases, we have a module in Python to work with the large files. The module we can use to handle large files is zipfile. By using this module, we can compress a large file into smaller files in Python. The following is the process that we have to follow for compressing the large files. ... Read More

How to convert JSON data into a Python object?

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

998 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

2K+ 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 do I look inside a Python object?

Niharika Aitam
Updated on 28-Feb-2025 16:27:27

239 Views

Python object is belongs to the object oriented programming language. We previously have idea and knowledge regarding the python object. provides several built-in functions that help you inspect various aspects of objects. Some of these methods, including help(), type(), dir(), id(), hasattr(), getattr(), and callable(). Using the help() function If we want to inspect the python object we have a built function of python named help(). That provides documentation about an object, including its methods and attributes. Example In the following code, we imported the power function from the math module. Next we applied the inbuilt function help() on power ... Read More

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

Niharika Aitam
Updated on 29-May-2025 11:24:49

389 Views

The class is a blueprint, which is used 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 an object-oriented programming language through a real-time scenario. Consider a library. In a library, we will have different numbers of books. Now we want to track every book from the library. For a book, we will have different attributes like book name, ... Read More

How to attach a C method to existing Python class?

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

247 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

Advertisements