Документация по Python

Создание методов классов в Python

В: Документация по Python

Создание пользовательских объектов метода

Пользовательские объекты методов могут быть созданы при получении атрибута класса (возможно, через экземпляр этого класса), если этот атрибут является объектом пользовательской функции, несвязанным объектом пользовательского метода или объектом метода класса.

class A(object):
    # func: A user-defined function object
    #
    # Note that func is a function object when it's defined,
    # and an unbound method object when it's retrieved.
    def func(self): 
        pass

    # classMethod: A class method
    @classmethod
    def classMethod(self):
        pass

class B(object):
    # unboundMeth: A unbound user-defined method object
    #
    # Parent.func is an unbound user-defined method object here,
    # because it's retrieved.
    unboundMeth = A.func

a = A()
b = B()

print A.func
# output: <unbound method A.func>
print a.func
# output: <bound method A.func of <__main__.A object at 0x10e9ab910>>
print B.unboundMeth
# output: <unbound method A.func>
print b.unboundMeth
# output: <unbound method A.func>
print A.classMethod
# output: <bound method type.classMethod of <class '__main__.A'>>
print a.classMethod
# output: <bound method type.classMethod of <class '__main__.A'>>


Когда атрибут является определяемым пользователем объектом метода, новый объект метода создается только в том случае, если класс, из которого он извлекается, совпадает или является производным классом класса, сохраненного в исходном объекте метода; в противном случае исходный объект метода используется как есть.

# Parent: The class stored in the original method object
class Parent(object):
    # func: The underlying function of original method object
    def func(self): 
        pass
    func2 = func

# Child: A derived class of Parent
class Child(Parent):
    func = Parent.func

# AnotherClass: A different class, neither subclasses nor subclassed
class AnotherClass(object):
    func = Parent.func

print Parent.func is Parent.func                # False, new object created
print Parent.func2 is Parent.func2              # False, new object created
print Child.func is Child.func                  # False, new object created
print AnotherClass.func is AnotherClass.func    # True, original object used 

Черепаха пример

Ниже приведен пример использования пользовательской функции, которая вызывается в скрипте несколько (∞) раз с легкостью.

import turtle, time, random #tell python we need 3 different modules
turtle.speed(0) #set draw speed to the fastest 
turtle.colormode(255) #special colormode
turtle.pensize(4) #size of the lines that will be drawn

 # This is our own function, in the parenthesis is a variable
 # we have defined that will be used in THIS FUNCTION ONLY.
 # This fucntion creates a right triangle
 
def triangle(size):
    turtle.forward(size) #to begin this function we go forward, the amount to go forward by is the variable size
    turtle.right(90) #turn right by 90 degree
    turtle.forward(size) #go forward, again with variable
    turtle.right(135) #turn right again
    
    # close the triangle. thanks to the Pythagorean theorem
    # we know that this line must be 1.5 times 
    # longer than the other two(if they are equal)
    turtle.forward(size * 1.5) 

while(1): #INFINITE LOOP
    turtle.setpos(random.randint(-200, 200), random.randint(-200, 200)) #set the draw point to a random (x,y) position
    turtle.pencolor(random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)) #randomize the RGB color
    triangle(random.randint(5, 55)) #use our function, because it has only one variable we can simply put a value in the parenthesis. The value that will be sent will be random between 5 - 55, end the end it really just changes ow big the triangle is.
    turtle.pencolor(random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)) #randomize color again 

Еще от кодкамп
Замечательно! Вы успешно подписались.
Добро пожаловать обратно! Вы успешно вошли
Вы успешно подписались на кодкамп.
Срок действия вашей ссылки истек.
Ура! Проверьте свою электронную почту на наличие волшебной ссылки для входа.
Успех! Ваша платежная информация обновлена.
Ваша платежная информация не была обновлена.