1class SillyClassName:
2 @classmethod
3 def my_name(cls_):
4 return cls_.__name__
5
6 def class_name(self):
7 # self.__class__ gets the current class
8 # .__name__ gets the name
9 return self.__class__.__name__
10
11SillyClassName.my_name()
12# prints SillyClassName
13
14inst = SillyClassName()
15inst.class_name()
16# prints SillyClassName
1class Dog:
2
3 def bark(self):
4 print("Woof!")
5
6 def roll(self):
7 print("*rolling*")
8
9 def greet(self):
10 print("Greetings, master")
11
12 def speak(self):
13 print("I cannot!")
14
15# Creating the Dog class instance and saving it to the variable <clyde>
16clyde = Dog()
17clyde.bark() # --> Woof!
18clyde.roll() # --> *rolling*
19clyde.greet() # --> Greetings, master
20clyde.speak() # --> I cannot!
21
22# Creating another Dog instance
23jenkins = Dog()
24jenkins.bark() # --> Woof!
25jenkins.roll() # --> *rolling*
26# .. And other methods
27# .. Infinite objects can be created this way, all implementing the same methods defined in our class
28
1class Person:#set name of class to call it
2 def __init__(self, name, age):#func set ver
3 self.name = name#set name
4 self.age = age#set age
5
6
7 def myfunc(self):#func inside of class
8 print("Hello my name is " + self.name)# code that the func dose
9
10p1 = Person("barry", 50)# setting a ver fo rthe class
11p1.myfunc() #call the func and whitch ver you want it to be with
1class Person:
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5 def myfunc(self):
6 print("Hello my name is " + self.name +".")
7
8p1 = Person("Victor", 24)
9p1.myfunc()