Python #ifdef

Grant Edwards invalid at invalid.invalid
Tue May 28 16:12:59 EDT 2013


On 2013-05-28, Carlos Nepomuceno <carlosnepomuceno at outlook.com> wrote:

> Thank you! I made it run like the following. What do you think about
> that? IS there a better way?
>
> #The following runs on Python 2.7
> sc3='''
> # Python 3
> def original(n):
> ??? m = 0
> ??? for b in n.to_bytes(6, 'big'):
> ??????? m = 256*m + b
> ??? return m
> '''
> if sys.version_info[0] == 3:
> ??? exec(sc3) 		 	   		  

You're trying to make this a lot harder than it really is:

if sys.version_info[0] == 3:
    def original(n):
        m = 0
        for b in n.to_bytes(6, 'big'):
            m = 256*m + b
        return m
else:
    def original(n):
        <something else?>
        

-- 
Grant Edwards               grant.b.edwards        Yow! Am I having fun yet?
                                  at               
                              gmail.com            



More information about the Python-list mailing list