-4

I am learning oop in python so i am having some problem to understand sefl keyword properly.

Suppose a program :

class ge:
   def __init__(self,a,b):
      self.p=a
      self.l=b
   def ff(self):
      aaa=self.p+self.l
      print(aaa)

hh=ge(1,2)
hh.ff()

I am confuse why its necessary to use any string with self with dot ? what it means ? Like:

self.a=a and we can change self.a to ay string like self.b , self.c what it means ?? why its necessary ?

My second question is :

what is difference between defining class with parameter and without parameter ?

class hello(object):
   def __init__(self,a,v):
      self.a=a
      self.v=v
   def p(self):
      f=self.a+self.v
      print(f)

he=hello(1,2)
he.p()

if i defined class hello(object) its working but if i defined class like: class hello(): its also working but if i defined like: class hello: its also working

what is the difference class hello(object): , class hello(), class hello:

Eleutetio
  • 139
  • 2
  • 8
  • 3
    Please ask only one question in one post, your second question could be asked in a new post. – kennytm Oct 31 '16 at 15:41
  • 1. `self` isn't a keyword, it's just an argument name that refers to the current instance of the class. You _could_ call it anything you like, but it's a convention to call it `self`. 2. In Python 3, there's no difference between `class hello(object):`, `class hello():`, or `class hello:`, but it's a good idea to use the 1st form if you want to write code that works correctly on Python 2 as well. – PM 2Ring Oct 31 '16 at 15:43
  • @PM2Ring please never mark duplicate if you don't understand question properly. i am not asking what is self , i know self make a attribute to global , i am asking something else. if you know what am asking then answer but don't mark unnecessary duplicate. – Eleutetio Oct 31 '16 at 16:02
  • @khelwood please never mark duplicate if you don't understand question properly. i am not asking what is self , i know self make a attribute to global , i am asking something else. if you know what am asking then answer but don't mark unnecessary duplicate – Eleutetio Oct 31 '16 at 16:03
  • "self make a attribute to global" I don't know exactly what you mean by that phrase, but it doesn't sound correct. There's a lot of good information in the target question linked at the top of this page, and in the questions linked in MMF's and Wonka's answers. Please study that information, and if you _still_ have a question on this topic that isn't covered by any of those answers, you can either add it to the bottom of this question and it may be re-opened, or you can ask a fresh question. – PM 2Ring Oct 31 '16 at 16:19
  • @kennytm i wanted quick answer and new user can ask only one question in 90 minute. – Eleutetio Oct 31 '16 at 20:27
  • @PM2Ring i didn't find what i was asking. If you know the answer please answer or let me ask question. i am not asking what is "self" and how its use. I am asking can we attach any string with self and how this string works ? – Eleutetio Oct 31 '16 at 20:28
  • I'm still not totally clear on what your actual question is. In your `ge.__init__` method, the statement `self.p=a` creates an attribute of the current instance of the `ge` object. That attribute is named 'p' and the assignment binds the `self.p` name to the object that was passed to `ge.__init__` in the `a` parameter. You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Nov 01 '16 at 04:37

2 Answers2

0

self is used to reference the instance of the class, is like this in Java

Duplicated: When do you use 'self' in Python?

Community
  • 1
  • 1
Wonka
  • 1,548
  • 1
  • 13
  • 20
0

First question : Duplicate of this question

Second question : There is no difference between the different notations. When you use the parenthesis it means that your class inherits from the class between parenthesis.

In python 3, by default every class inherits from the class object. So hello(object): , class hello():, class hello: are totally equivalent. In python 2 however, you must explicit the inheritance.

Here are more details on how to create classes in python.

Community
  • 1
  • 1
MMF
  • 5,750
  • 3
  • 16
  • 20
  • 1
    "by default every class inherits from the class object" That's true in Python 3, but not in Python 2, which is why you need to explicitly inherit from `object` if you want a [new-style class](https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes) (and you probably do). – PM 2Ring Oct 31 '16 at 15:53
  • @PM2Ring Thanks for the precision ! I edited my answer. – MMF Oct 31 '16 at 15:57