Skip to content

Commit

Permalink
object
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza4096 committed Dec 2, 2022
1 parent 81c90c1 commit 444137d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Using *args and **kwargs to set values of object
# *args receives arguments as a tuple.
# **kwargs receives arguments as a dictionary.

class car(): #defining car class
def __init__(self,*args): #args receives unlimited no. of arguments as an array
self.speed = args[0] #access args index like array does
self.color=args[1]

#creating objects of car class

audi=car(200,'red')
bmw=car(250,'black')
mb=car(190,'white')

print(audi.color)
print(bmw.speed)
print(mb.__dict__)

# With **kwargs
class car2(): #defining car class
def __init__(self,**kwargs): #args receives unlimited no. of arguments as an array
self.speed = kwargs['s'] #access args index like array does
self.color = kwargs['c']

#creating objects of car class

audi=car2(s=200,c='red')
bmw=car2(s=250,c='black')
mb=car2(s=190,c='white')

print(audi.color)
print(bmw.speed)
print(mb.__dict__)

0 comments on commit 444137d

Please sign in to comment.