Guy Brewin
0
Q:

python object

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

    def myfunc(self):
      print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc() 
43
class Person:
  def __init__(self, _name, _age):
    self.name = _name
    self.age = _age
   
  def sayHi(self):
    print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
    
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
17
class MyClass(object):
  def __init__(self, x):
    self.x = x
11
class Mammal:
    def __init__(self, name):
        self.name = name

    def walk(self):
        print(self.name + " is going for a walk")


class Dog(Mammal):
    def bark(self):
        print("bark!")


class Cat(Mammal):
    def meow(self):
        print("meow!")


dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
12
class Person:#set name of class to call it 
  def __init__(self, name, age):#func set ver
    self.name = name#set name
    self.age = age#set age
   

    def myfunc(self):#func inside of class 
      print("Hello my name is " + self.name)# code that the func dose

p1 = Person("barry", 50)# setting a ver fo rthe class 
p1.myfunc() #call the func and whitch ver you want it to be with 
14
# To create a simple class:
class Shape:
  	def __init__():
      	print("A new shape has been created!")
      	pass
    
    def get_area(self):
		pass

# To create a class that uses inheritance and polymorphism
# from another class:
class Rectangle(Shape):
  
	def __init__(self, height, width): # The constructor
    	super.__init__()
        self.height = height
    	self.width = width

	def get_area(self):
      	return self.height * self.width
12

  class MyClass:
  	x = 5
    def __init__(self, name):
      self.name=name
    def sayname(self):
      print(self.name)
    def getnum(self):
      return x
  y=MyClass("Henry")
  y.sayname()
  print(y.getnum()*5)
 
  
9
#NOTES
class PartyAnimal:
  	def Party():
      #blahblah

an=PartyAnimal()

an.Party()# this is same as 'PartyAnimal.Party(an)'
3
#objects are collections of data
2
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
  	def __init__(self, var1=0, var2):
    
    	#the name of the construct must be "__init__" or it won't work
    	#the arguments "self" is mandatory but you can add more if you want 
    	self.age = var1
    	self.name = var2
    
    	#the construct will be execute when you declare an instance of this class
    
  	def otherFunction(self):
    	
        #the other one work like any basic fonction but in every methods,
    	#the first argument (here "self") return to the class in which you are
 	
4

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

  
print(p1.name)
print(p1.age) 
0

New to Communities?

Join the community