Python class Variables And Functions

📔 : Python 🔗

Python class Defining Variables

Python में classes के अंदर variables को बैसे ही define किया जाता है , जैसे हम normally करते हैं। बस variable को कुछ initial value assign जरूर होनी चाहिए।
For Example -

class MyClass :
   name = None
   age = None

Python class Defining Method

method define करते समय एक default parameter set किया जाता है जो current class object को represent करता है। इसी default parameter की help से class variables और दुसरे methods को access करते हैं।

Python class Variables And Methods Example

Copy Fullscreen Close Fullscreen Run
class myClass :
  # declare variables.
  name = None
  age = None

  # defining constructor.
  def __init__(self, name, age) :
    # now assign these values to class variables name , age .
    self.name = name
    self.age = age

  # now define method to get name , age.
  def get_info(self) :
    print('Name : ', self.name)
    print('Age : ', self.age)

# making class object and pass values.
myobj = myClass("Rahul Kuamr", 24)
# now call the method.
myobj.get_info()
Output
C:\Users\Rahulkumar\Desktop\python>python class_member.py
Name :  Rahul Kuamr
Age :  24

जैसा कि आपने पिछले topic में पड़ा कि , class constructor मतलब __init__() optional होता है , आप चाहे तो इसे define न करें तो भी कोई problem नहीं है।


आप direct function में variables pass कर सकते हैं। लेकिन फिर class object बनाते समय constructor में values नहीं pass करनी पड़ेगीं।
For Example

Copy Fullscreen Close Fullscreen Run
class myClass :
  # define method to print name , age.
  def print_info(self, name, age) :
    print('Name : ', name)
    print('Age : ', age)

# making class object.
myobj = myClass()
# now call the method and pass the values.
myobj.print_info("Rahul Kuamr", 24)
Output
C:\Users\Rahulkumar\Desktop\python>python class_member.py
Name :  Rahul Kuamr
Age :  24

Python Updating class Variable

किसी class variable को directly कोई new value assign करके update कर सकते हैं।
See Example -

Copy Fullscreen Close Fullscreen Run
class myClass :
  name = 'Rahul'

# make the object.
myobj = myClass()
print('Name before update : ', myobj.name)

# update variable value.
myobj.name = 'Rahul Kumar rajput'
print('Name after update : ', myobj.name)
Output
C:\Users\Rahulkumar\Desktop\python>python class_member_del.py
Name before update :  Rahul
Name after update :  Rahul Kumar rajput

Python Adding new variable to object

इसी तरह किसी new variable को class object में add कर सकते हैं।
See Example

myobj = myClass()
myobj.age = 24
myobj.country = 'India'

Python Static Methods

अभी आपने जो उपर define किये गए method examples देखे थे वो सभी non - static methods थे। इन्हे बिना class object initialize किये access नहीं किया जा सकता है। इसलिए non - static methods में पहला parameter self रखते हैं जो current object को represent करता है। जिससे class variables और other methods को access किया जा सके।


बैसे other object oriented programming languages (PHP, JAVA) में Static Methods define करने के लिए static keyword का use किया जाता है , लेकिन Python में Static Methods define करने के लिए simply method define करते समय default argument self ( जो current object को represent करता है। ) को define नहीं करते हैं।

Note* static methods को आप बिना Object create किये directly class name के साथ access किया जाता है। अब चूंकि Class Object create किया नहीं जाता है इसलिए static methods में current object access नहीं कर सकते हैं। और इसीलिए class constructor __init__() run नहीं होता है।

Python Static Method Example

Copy Fullscreen Close Fullscreen Run
class A :
  name = "Rahul Kumar"
  def __init__(self) :
    print('Construcotr Running.')

  #it is a static method , because there is no self parameter to access current object.
  def sayhi() :
    print('Hello !.')

# access method and variables directly.
A.sayhi()
print(A.name)
Output
C:\Users\Rahulkumar\Desktop\python>python class_ex.py
Hello !.
Rahul Kumar

इसी तरह से अगर आपको की static method में class का दूसरा कोई static method या variable access करना हो तो class name के साथ ही होगा।
For Example -

 def sayhi() :
    #access class variable
    print('Hello ', A.name)

    #access static method.
    A.static_method()

Important
Static Methods में आप कभी भी non static method call नहीं कर सकते हैं क्योंकि Static Methods Object scope में नहीं होता हैं। हाँ Non - Static methods में आप Static Methods को easily call कर सकते हैं, लेकिन Class Name के साथ।

class A :
  name = "Rahul Kumar"
  def __init__(self) :
    print('Construcotr Running.')

  #static method.
  def sayhi() :
    print('Hello ', A.name)
  
  #non static method.
  def myfun(self) :
      #access static method.
      A.sayhi()
  
# access no static method.
a = A()
a.myfun()

Output : 
Construcotr Running.
Hello  Rahul Kumar

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers