Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

creating class and object in python

class Parrot:

    # class attribute
    species = "bird"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
Comment

Creating Class and Object in Python

class Parrot:
    # class attribute
    species = "bird"
    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
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

creating class and object in python

class Parrot:

    # class attribute
    species = "bird"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
Comment

what is an object in python

#objects are collections of data
Comment

Creating Class and Object in Python

class Parrot:
    # class attribute
    species = "bird"
    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
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

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

Creating an Object in Python

>>> harry = Person()
Comment

what is an object in python

#objects are collections of data
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

Creating an Object in Python

>>> harry = Person()
Comment

PREVIOUS NEXT
Code Example
Python :: activate venv environment 
Python :: how to encode a string in python 
Python :: python3 conditional with boolean 
Python :: how to access a txt file through os library in python 
Python :: Python get the name of the song that is playing 
Python :: separate each characters by commas into a single characters separated by commas 
Python :: check if a string is palindrome or not using two pointer function in python 
Python :: quotation marks n string 
Python :: algebraic pyramid python 
Python :: list vs dictionary python 
Python :: extract specific key values from nested dictionary 
Python :: Python use number twice without assignment 
Python :: set difference in multidimensional array numpy 
Python :: looping through strings 
Python :: ipywidgets label text color 
Python :: Python - Comment lire une ligne de fichier par ligne 
Python :: python how to add a new key to a dictionary 
Python :: connect to vvenv python 
Python :: ensemble model using voting classifier 
Python :: insert a new row to numpy array in especific position 
Python :: us staes python 
Python :: binary tree python implementation 
Python :: how to find out the max and min date on the basis of property id in pandas 
Python :: hh:mm to mins in python 
Python :: how to create a function in python 
Python :: python open file location 
Python :: PySimpleGUI all elements 
Python :: linear search implementation 
Python :: ipython play audio 
Python :: set comprehension 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =