Matt
0
Q:

class and object in python

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 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
#NOTES
class PartyAnimal:
  	def Party():
      #blahblah

an=PartyAnimal()

an.Party()# this is same as 'PartyAnimal.Party(an)'
3
class uneclasse():
  def __init__(self):
    pass
  def something(self):
    pass
xx = uneclasse()
xx.something()
1
class car():
  gas=100
  def __init__(self, mpg, position, gast):
    gas=gast
    self.mpg=mpg
    self.position=position
  def move(self, miles):
    if (self.mpg*gas>miles):
      print("You don't have enough gas")
    elif(self.mpg*gas<=miles):
      self.position+=miles
      gas-=(1/self.mpg)*miles
  def get_gas(self):
    	if(self.position %50==0):
        	gas=100
        else:
          	print("You can't do that")
class Truck(car):
  def __init__(self, carry_capacity):
    	self.carry_capactiy=carrycapacity
  def carry(self, load):
    	if(load<=carry_capacity):
        	print("Successfully carried")
        else:
          	print("Failed to carry")
Car=car(5, 0, 100)
Car.move(50)
Car.get_gas()
truck=Truck(5)
truck.get_gas()
truck.carry()

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

p1 = Person("John", 36)

print(p1.name)
print(p1.age)
1
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named 
attributes. The point of a class is to call the class later allowing you 
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
0
>>> class Snake:
...     pass
... 
>>> snake = Snake()
>>> print(snake)
<__main__.Snake object at 0x7f315c573550>
3

New to Communities?

Join the community