Does Python have properties?

So something like:

vector3.Length

which is actually a function call that calculates the length of the vector, not a variable.

+3
source share
5 answers

With new style classes you can use property(): http://www.python.org/download/releases/2.2.3/descrintro/#property .

+14
source

3 , , - :

import math
vector3 = [5, 6, -7]
print math.sqrt(vector3[0]**2 + vector3[1]**2 + vector3[2]**2)

, , :

import math
vector3 = [5, 6, -7]
print math.sqrt(sum(c ** 2 for c in vector3))

Length :

import math
class Vector3(object):
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z
  @property
  def Length(self):
    return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
vector3 = Vector3(5, 6, -7)
print vector3.Length
+5

, (), . - , , .

+3

, , . python

, , , .

0

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


All Articles