Answers for "python class metod"

19

python class

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
Posted by: Guest on June-12-2020
0

python classmethod

# 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
Posted by: Guest on December-03-2020

Python Answers by Framework

Browse Popular Code Answers by Language