Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

class python

class MyClass(object):
  def __init__(self, x):
    self.x = x
Comment

class python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40
Comment

class python

class Employee(Object)
	def __init__(self, name, age, salary):
    	self.name = name
      	self.age = age
        self.salary = salary
        
        
  
  	def __str__(self)
    return f"Employee {name} 
hes age {age} 
and make {salary}"
Comment

Class in python

# Node class
class Node:
  
    # Function to initialize the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize
                          # next as null
  
# Linked List class
class LinkedList:
    
    # Function to initialize the Linked
    # List object
    def __init__(self):
        self.head = None
Comment

object function in Python

class Student:
	def __init__(self, name, gpa, major):
    self.name = name
    self.gpa = gpa
    self.major = major
    
   	def is_good_student(self):
    	if self.gpa > 3.2:
        	return True
        else:
        	return False
Comment

what is an object in python

#objects are collections of data
Comment

class in python

class LuckyLemur():
  	def __init__(self, description):
		self.description = description
	
    def reveal_description(self):
    	print(f'Lucky Lemur is {self.description}')

lucky_lemur = LuckyLemur('Pro')
lucky_lemur.reveal_description()
Comment

simple python class

class Person:
    def say_hi(self):
        print('Hello, how are you?')

p = Person()
p.say_hi()
# The previous 2 lines can also be written as
# Person().say_hi()
Comment

object python

# ---------------------------------------- Object Oriented Programming ------------------------------------- #
# It Is Based On Three Pillars =  Encapsulation = ( self ) / -  Inheritance / -  Polymorphism /
# Constructor ==> __init__(self) / self => Default Parameter // Self Can Be Named AnyThing You Need 
# Class ==> / Like The Real Class And You Putted Inside Her Somethings Like = ( Constructor, Methods, Attributes ) 
# print( classname.__class__ ) ==> / If You Need To Know THe Class Follow What ? 
# If You Wanna Run The Class You Need To Call Her In Variable = ( x = ClassName 
 x.Thing )
# Inheritance ==> / If You Have Class In Your Project And You Wanna Put In Something
#  You Create The New Class And Give Her The Class Name You Need As A Parameter 
# Polymorphism Take The Same Elements From The Inheritance But Do Something Deference Or Any Some Like
#  ==> + Between The Numbers Do Addition But Between Strings Do Concatenation
# super() ==> If You Need Inheriting A Constructor From Another Class :-
#   You Need Use super() Method / :-
#   You Type InSide Your Class Under You //> __Init__( self, What You Need Inheriting From Old Class ) :
#         super( Your Class, self ).__init__( What You Need Inheriting From Old Class, And Your New Additions ) :
#         If you Need Addition Something Like :
#           print( "Hello, Im From 'Super'" )
# Inheriting From Class Or More :-
#   If We Have 4 Class A,B,C,D 
#       A Inside Her Function Do ==> ( "Doing From 'A'" )
#       B Inheriting From A
#       C Inside Her Function Do ==> ( "Doing From 'C'" )
#       D Inheriting From B,A
# If We Run The Function Inside D Will Print ==> Doing From 'A'
# Because He Start From 'B' Because 'B' The First Class 'D' Inheriting From, After This Go 
# To 'A' // Because 'B' Inheriting From 'A'  Will Print => Doing From 'A' /
# If If He Does Not Find Anything In 'A'
# He Back And Go To 'C'
# If You Need Know The Way Just Type ==> print( TheClassName.mro() )
Comment

class python

"""
:return : Simple class methods with an alternative constructor, just only take
		  an input of the data in the form of string and return it into the 
          instance variable.
"""

class Employees:
    no_of_leaves = 8

    def __init__(self, _age, _name, _leave, _salary, _work_experience):
        self.name = _name
        self.age = _age
        self.leave = _leave
        self.salary = _salary
        self.work_experience = _work_experience
        
    @classmethod
    def alternative__constructor_class(cls, _input_string):
        """
        :param _input_string: Takes the input from the function from the class,
        :return: The alternative__constructor works to add data only one string and then,
                 return it into the object.
        :parameter: Class methods as the alternative constructor.

        """

        # params_split = _input_string.split("-")
        # print(params_split)
        # return cls(params_split[0], params_split[1], params_split[2], params_split[3], params_split[4])
        # Multi liner functions
        return cls(*_input_string.split("-"))

    @classmethod
    def change_leave_class_method(cls, _new__leave):
        cls.no_of_leaves = _new__leave

    def return__employee_statement(self):
        return f"Name : {self.name} Age : {self.age} leave : {self.leave} salary : {self.salary}" 
               f"Work Experience : {self.work_experience}"


anshu = Employees(_name="Anshu", _salary=70000, _leave=8, _work_experience=6, _age=16)
shivam = Employees.alternative__constructor_class("shivam-16-7000-4-2")
print(shivam.return__employee_statement())

anshu.change_leave_class_method(30)
print(Employees.no_of_leaves)
Comment

class python example

def ok(index):
    print(index) > Hi!
ok("Hi!")
class Lib:
    def ok(self,Token):
        print(Token) > Hello World!
Library = Lib()
Library.ok("Hello World!")
Comment

PREVIOUS NEXT
Code Example
Python :: pairs with specific difference 
Python :: how to separate numeric and categorical variables in python 
Python :: Python list loop tutorial 
Python :: Reverse an string Using Reversed function 
Python :: nested list comprehension python 
Python :: python turtle tutorial 
Python :: tf dataset 
Python :: login required 
Python :: Python NumPy delete Function Example 
Python :: test pypi 
Python :: class inheritance 
Python :: how to convert time from one timezone to another in python 
Python :: Routes In Django 
Python :: variables in python 
Python :: python in 
Python :: standard error of mean 
Python :: path in python 
Python :: densenet python keras 
Python :: how to convert a list to a string in python 
Python :: turn numpy function into tensorflow 
Python :: join tables in django orm 
Python :: stack.pop() 
Python :: python double underscore methods 
Python :: numpy.dot 
Python :: del(list) python 
Python :: What Is Python Recursive Function in python 
Python :: 2--2 in python prints? 
Python :: pydantic numpy ndarray type 
Python :: codegrepper is cool 
Python :: summary r language equivalent in python 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =