Skip to content

Instantly share code, notes, and snippets.

@blude
Created February 2, 2020 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blude/a0ad29583d75776ef9226806d45b443d to your computer and use it in GitHub Desktop.
Save blude/a0ad29583d75776ef9226806d45b443d to your computer and use it in GitHub Desktop.
python examples - nested functions and classes
"""
Resources:
----------
1. Nested functions https://stackabuse.com/python-nested-functions/
2. Packing problem: https://en.wikipedia.org/wiki/Packing_problems
- Answer on SO: https://stackoverflow.com/questions/7392143/python-implementations-of-packing-algorithm
"""
"""
Example #1: nested function
"""
# Given a point
point_x = rg.Point3D(...)
def modify_vertex(point):
# Create the inner function
def offset(distance):
# ...and do something here
return rg.Point3D(point + distance)
return offset
point_a = modify_vertex(point_x)
point_b = modify_vertex(point_x)
point_a(100)
point_b(200)
"""
Example #2: Nested function with 2 parameters
"""
# Given a point in a list of points
point_n = point_list[i]
def origin(point_a):
point_a.Z += 10
# Create the inner function
def triangulo(point_b, point_c):
return rg.Triangulo(pontoA, pontoB, pontoC)
return triangle
triangle = origin(point_n)
triangle(point_list[n - 1], point_list[n + 1])
"""
Example #3: Class creation
"""
class Wall(object):
def __init__(self, width, height):
self.width = width
self.height = height
def increase(self, length):
self.width = self.width + length
room_wall = Wall(100, 200)
room_wall.increase(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment