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

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 :: Python How To Convert Text to Speech 
Python :: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. 
Python :: get length from variable python 
Python :: reading a dataset in python 
Python :: python chatbot api 
Python :: python input - how to read a number 
Python :: #math function in python: 
Python :: add dataframe column to set 
Python :: how to replace special characters in a string python 
Python :: combination in python math 
Python :: python x = x + 1 
Python :: python __new__ 
Python :: Open the .txt file 
Python :: python 3.3 release date 
Python :: how to if in pythob 
Python :: separate digits with comma 
Python :: How to code a simple rock, paper, scissors game on Python 
Python :: indent python 
Python :: series change index pandas 
Python :: how to flatten list of lists in python 
Python :: Examples using matplotlib.pyplot.quiver 
Python :: sort array numpy 
Python :: TypeError: can only concatenate str (not "method") to str 
Python :: image analysis python 
Python :: renamecolumns pandas 
Python :: printing in python 
Python :: simple click counter in python 
Python :: manifest.in python 
Python :: pystache unescaped characters 
Python :: python from string to bytes to hex 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =