S. Kohn
0
Q:

python: @classmethod

class MyClass(object):
  def __init__(self, x):
    self.x = x
11
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40
3
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

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

    def speak(self):
        print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')

JUB0T = Dog('JUB0T', 55)
Friend = Dog('Doge', 10)
JUB0T.speak()
Friend.speak()
0
class Animal(object): # Doesn't need params but put it there anyways.
    def __init__(self, species, price):
        self.species = species # Sets species name
        self.price = price # Sets price of it
    
    def overview(self): # A function that uses the params of the __init__ function
        print(f"This species is called a {self.species} and the price for it is {self.price}")

class Fish(Animal): # Inherits from Animal
    pass # Don't need to add anything because it's inherited everything from Animal
 
salmon = Fish("Salmon", "$20") # Make a object from class Fish
salmon.overview() # Run a function with it
dog = Animal("Golden retriever", "$400") # Make a object from class Animal
dog.overview() # Run a function with it
8
class Float:
    def __init__(self, amount):
        self.amount = amount

    def __repr__(self):
        return f'<Float {self.amount:.3f}>'

    @classmethod
    def from_sum(cls, value_1, value_2):
        return cls(value_1 + value_2)


class Dollar(Float):
    def __init__(self, amount):
        super().__init__(amount)
        self.symbol = '€'

    def __repr__(self):
        return f'<Euro {self.symbol}{self.amount:.2f}>'


print(Dollar.from_sum(1.34653, 2.49573)) 
1
class point:
	def __init__(self, x, y):
    	self.x = x
        self.y = y
        
    @classmethod
    def zero(cls):
    	return cls(0, 0)
        
    def print(self):
    	print(f"x: {self.x}, y: {self.y}")
        
p1 = point(1, 2)
p2 = point().zero()
print(p1.print())
print(p2.print())
0
# classmethod example
In [20]: class MyClass:
    ...:     @classmethod
    ...:     def set_att(cls, value):
    ...:         cls.att = value
    ...:

In [21]: MyClass.set_att(1)

In [22]: MyClass.att
Out[22]: 1

In [23]: obj = MyClass()

In [24]: obj.att
Out[24]: 1

In [25]: obj.set_att(3)

In [26]: obj.att
Out[26]: 3

In [27]: MyClass.att
Out[27]: 3
0
class A:        # define your class A
.....

class B:         # define your class B
.....

class C(A, B):   # subclass of A and B
  
obj = C() #to create instance
# issubclass(sub, sup) boolean function returns true if the given 
# subclass sub is indeed a subclass of the superclass sup

# isinstance(obj, Class) boolean function returns true if obj is an 
# instance of class Class or is an instance of a subclass of Class
0

New to Communities?

Join the community