100% found this document useful (1 vote)
48 views

Data Structure in Python Dictionary Object in Class:: - Init

The document discusses different ways to create and use data structures like dictionaries and lists in Python classes. It shows how to create a dictionary using a class, create list objects using classes by inheriting from the built-in list class and initializing an empty list, and also creating a list inside a class. It also demonstrates using decorators to modify functions in Python.

Uploaded by

pawan verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
48 views

Data Structure in Python Dictionary Object in Class:: - Init

The document discusses different ways to create and use data structures like dictionaries and lists in Python classes. It shows how to create a dictionary using a class, create list objects using classes by inheriting from the built-in list class and initializing an empty list, and also creating a list inside a class. It also demonstrates using decorators to modify functions in Python.

Uploaded by

pawan verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Data structure in Python

Dictionary object in class:


class Sample:
def __init__(self):
self.add = dict()

def build(self,args):
self.add.update(args)

s = Sample()
key=[1,2,3]
value=['a','b','c']
s1={k:v for (k,v) in zip(key,value)}
s.build(s1)
print( s.add)

output: {1: 'a', 2: 'b', 3: 'c'}

CREATING LIST OBJECT USING CLASS


class Listobj:
def __init__(self):
self.list=[]
def add(self,arg):
self.list.append(arg)
list=[1,2,3,4,5]
s=Listobj()
for i in list:
s.add(i)
print(s.list)

or
class Listobj(list):
def __init__(self):
self=[]
def add(self,arg):
self.append(arg)
list=[1,2,3,4,5]
s=Listobj()
for i in list:
s.add(i)
print(s)
# CREATING LIST INSIDE A CLASS

class Listobj:
list=[]
def add(self,value):
self.list.append(value)
obj=Listobj()
obj.add('pawan')
print(obj.list) #using object
print(Listobj.list) #using class

class Listobj:
list=[]
def add(self,value):
self.list.append(value)
obj=Listobj()

newlist=['a','b','c','d']
for i in newlist:
obj.add(i)

print(obj.list) #using object


print(Listobj.list) #using class

DECORATORS IN PYTHON
def makepretty(func):
def inner():
print("i'm innner")
func()
return inner
@makepretty
def ordinary():
print("i'm just ordinary")
ordinary()

def smartdivide(func):
def inner(a,b):
print("division starts here...")
if b==0:
print("oops cant divide it man")
return
return func(a,b)
return inner
@smartdivide
def devide(a,b):
return a/b
print(devide(4,0))

You might also like