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

what is an object in python

#objects are collections of data
Comment

python object creation

def dump(obj):
  for attr in dir(obj):
    print("obj.%s = %r" % (attr, getattr(obj, attr)))
Comment

Creating an Object in Python

>>> harry = Person()
Comment

PREVIOUS NEXT
Code Example
Python :: hash in python 
Python :: migration django 
Python :: adding an item to list in python 
Python :: upload image to s3 python 
Python :: arithmetic operators in python 
Python :: def rectangles 
Python :: shape function python 
Python :: count python 
Python :: python __new__ 
Python :: runtime errors in python 
Python :: python function return function 
Python :: how to make an argument optional in python 
Python :: how to update image in django 
Python :: partition python 
Python :: List Nested Lists 
Python :: how to sum only the odd values in python 
Python :: odd number sum in python 
Python :: what does manage.py do 
Python :: csv reader url 
Python :: python include file 
Python :: nested python list 
Python :: optimization in python 
Python :: what mean import in python 
Python :: select first row of every group pandas 
Python :: Reverse an string Using Reversed 
Python :: hur många partier sitter i riksdagen 
Python :: significant figures on axes plot matplotlib 
Python :: light fm cold start problem 
Python :: Installez django-cruds-adminlte 
Python :: command to update pip 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =