- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Niharika Aitam](https://www.tutorialspoint.com/assets/profiles/654775/profile/60_3867437-1684125242.jpg)
764 Views
There are different modules or packages in the python classes. When we use their names as it is in the code it will be somewhat clumsy and not good to see. So, we need to organize the python classes in modules and packages. Modules are the group of functions, classes or any block of code kept in a single file. The file extension of the methods will be .py. If the python code is with 300-400 lines of code, then it can be made as a module for better understandability. The module name can be available as ... Read More
![Niharika Aitam](https://www.tutorialspoint.com/assets/profiles/654775/profile/60_3867437-1684125242.jpg)
565 Views
In python we have different methods available to make our work very easy and simple. Among the methods available in python we have two methods namely getter and setter. These methods play a vital role in the object oriented programming language of python to hide the private variables. These methods of python are not as same as the getters/setters methods in other object oriented programming languages. These are used in object oriented programming language for data encapsulation. These are used with the below conditions The getters/setters in python are used to validate the logic for getting and ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
751 Views
To instantiate the python class, we need to get the class name first. This is achieved by following codedef get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m = __import__( module ) for comp in parts[1:]: m = getattr(m, comp) return mm is the classWe can instantiate this class as followsa = m() b = m(arg1, arg2) # passing args to the constructor
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
10K+ Views
InheritanceBeing an Object Oriented language, Python supports inheritance, it even supports multiple inheritance. Classes can inherit from other classes. A class can inherit attributes and behaviour methods from another class, called the superclass. A class which inherits from a superclass is called a subclass, also called heir class or child class. In other words inheritance refers to defining a new class with little or no modification to an existing class.class A: # define your class A pass class B: # define your class B pass class C(A, B): # subclass of A ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
160 Views
The following code prints the list of functions of the given class as followsExampleclass foo: def __init__(self): self.x = x def bar(self): pass def baz(self): pass print (type(foo)) import inspect print(inspect.getmembers(foo, predicate=inspect.ismethod))OutputThe output is [('__init__', ), ('bar', ), ('baz', )]
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
10K+ Views
Given a string as user input to a Python function, I'd like to get a class object out of it if there's a class with that name in the currently defined namespace.Exampleclass Foobar: pass print eval("Foobar") print type(Foobar)Output __main__.Foobar Another way to convert a string to class object is as followsExampleimport sys class Foobar: pass def str_to_class(str): return getattr(sys.modules[__name__], str) print str_to_class("Foobar") print type(Foobar)Output__main__.Foobar
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
907 Views
We can use python-jsonschema-objects which is built on top of jsonschema.The python-jsonschema-objects provide an automatic class-based binding to JSON schemas for use in Python.We have a sample json schema as followsschema = '''{ "title": "Example Schema", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
10K+ Views
A global variable is a variable with global scope, meaning that it is visible and accessible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global scope of the program.We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.ExampleExample of global variable declarationdef f(): global s print(s) ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
352 Views
As per Python documentation ‘super’ can help in extending multiple python classes in inheritance. It returns a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.In other words, a call to super returns a fake object which delegates attribute lookups to classes above you in the inheritance chain. Points to note:This does not work with old-style classes.You need to pass your own class and ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
304 Views
A class attribute exists until the last reference goes away. A global variable also exists until the last reference goes away. Neither of these are guaranteed to last the entire duration of the program.Also, a class defined at module scope is a global variable. So the class (and, by implication, the attribute) have the same lifetime as a global variable in that case. If no instances of the class are currently live, the class and its class attributes might be garbage-collected if their reference counts become zero.