• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
samgj

Redefining a Function

5 posts in this topic

In Python after a function has already been declared can it be redefined without it being completely redefined. Could I for example create a function then add or remove a statement from it?

 

For example say I have this:

def func(x, y):
    y = 2 * x
    return y

Then could I do something like this:

func() = func() - return y

So then the function would be only:

    y = 2 * x
0

Share this post


Link to post
Share on other sites
The short answer is no. Once you create a python function it gets transformed into a compiled code object, and there is no simple way to modify the code object.
 
However, you can use decorators to wrap functions to add functionality. It's also possible to compile python source to an AST and modify the AST such as with the ast module. If feeling really ambitious, the byte code for a code object can be seen via the co_code member of a code object, and that byte code could be manually manipulated. Doing so would have no guarantees of portability between python versions (including minor point versions). You may want to investigate the Python language services modules for more information about your options for dealing with compiled functions.
0

Share this post


Link to post
Share on other sites
First of all, your example is wrong: the function should not receive y as a parameter, as "y=2*x" defines a local variable named y. If you want to alter a global variable called y, you need to put "global y" inside your function to override the interpreter assumption that names are local variables.
C:\windows\system32>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> y=5
>>> def double1(x):
...     print(y)
...     y=x*2
...     print(y)
...
>>> print(y)
5
>>> double1(25)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in double1
UnboundLocalError: local variable 'y' referenced before assignment
>>> def double2(x):
...     y=x*2
...     print(y)
...
>>> double2(25)
50
>>> print(y)
5
>>> def double3(x):
...     global y
...     y=x*2
...     print(y)
...
>>> double3(13)
26
>>> print(y)
26
Functions and classes are perfectly well behaved objects that you can replace [b]as a whole[/b], like this:.
C:\windows\system32>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(x):
...     return x+1
...
>>> def g(x):
...     return x+2
...
>>> print(f(1))
2
>>> print(g(1))
3
>>> f,g = g,f
>>> print(f(1))
3
>>> print(g(1))
2
>>> f=None
>>> print(f(1))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'NoneType' object is not callable
However, classes and functions are for practical purposes global data, and altering them might be a bad idea: conflicts between call sites who want different variations of the function are likely. Consider more mainstream solutions like using different functions or using additional parameters to get different behaviour.
0

Share this post


Link to post
Share on other sites

Firstly, the example given is ridiculous, because there is no benefit for having a function that's the same as another, except for the fact that it doesn't return a value. You can just not use the result in the caller. The overhead for returning a value is negligible, if not 0.

 

Secondly, a more general way of making functions out of other functions, is to have the larger function call the more specific functions it needs. The the user can call either the larger function, or the more specific function, depending on what is needed. The decorator's that SiCrane mentioned is a form of this.

0

Share this post


Link to post
Share on other sites
code = \
"""
def func(x, y):
	y = 2 * x
	return y
"""

code = code.split('\n')
code.pop(); code.pop(0)

exec("\n".join(code))

func(3, 2) # 6

exec("\n".join(code[:2])) # Only first 2 lines, excluding return

func(3, 2) # None

But don't ever do this. biggrin.png

0

Share this post


Link to post
Share on other sites

 

In Python after a function has already been declared can it be redefined without it being completely redefined. Could I for example create a function then add or remove a statement from it?

 

For example say I have this:

def func(x, y):
    y = 2 * x
    return y

Then could I do something like this:

func() = func() - return y

So then the function would be only:

    y = 2 * x

I think an important question here is, _why_ do you want to do this? I agree with King Mir--the example is not very realistic.

 

So what are you trying to do? Are you just trying to achieve partial reuse of a function? (If that's the case, you're better off just breaking this function into 2 or more, so that you can compose the individual parts for use in other situations.)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0