Initial Questions About Python Classes

I am new to Python, so please don't cry if I ask for something too noobish :)

1.

I have a class:

class Test:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def wow():
        print 5 * 5

Now I am trying to create a class object:

x = Test(3, 4)

This works as expected. However, when I try to call the wow () method, it returns an error, which is fixed by changing wow () to:

def wow(self)

Why do I need to enable self, and if not, what does this method mean?

2. In the definition __init__:

def __init__(self, x, y):
   self.x = x
   self.y = y

Why do I need to declare x and y when I can do this:

def __init__(self):
    self.x = x
    self.y = y

I hope I clear up ...

Thank you for your time.

+3
source share
6 answers

Python . , , , , , , .

x y , , , .

+2

:

def __init__(self):
  self.x = x
  self.y = y

gobal vars x y ( )

:

def __init__(self, x, y):
      self.x = x
      self.y = y

,

: -)

+4

,

x y, :

def __init__(self):
    self.x = x
    self.y = y

, x y - , () .

>>> class c:
    def __init__(self):
        self.x = x

>>> x = 1
>>> q = c()
>>> q.x
1
>>> del x
>>> q.x
1
>>> w = c()

Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    w = c()
  File "<pyshell#14>", line 3, in __init__
    self.x = x
NameError: global name 'x' is not defined
>>> 
>>> w = c(2)

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    w = c(2)
TypeError: __init__() takes exactly 1 argument (2 given)

/ - , , , "

+1

self - "" - , . , / . Ipython:

In [66]: class Test:
   ....:     def __init__(self):
   ....:         pass
   ....:     def wow(self):
   ....:         print self
   ....:
   ....:

In [67]: x = Test()

In [68]: x.wow()
<__main__.Test instance at 0x0159FDF0>

, x y .

, :

class Test:
    def __init__(self):
        self.x = x
        self.y = y

x = Test()

NameError.

, :

x = 3
y = 4
test = Test()

. . 2:

In [72]: import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let do more of those!
+1
  • Python " " , , . , Java, # .., Python .

  • . x y ? , , , . - .

0

Python , ++ Java, , .

, , , , (, ), .

In C ++ and Java, you have an implicit "this" link, which is present in the compiled version of your program, but not in the source code. You use the static keyword to turn it into a class method that does not have "this".

0
source

Source: https://habr.com/ru/post/1750300/


All Articles