logo
down
shadow

Converting Python 2.x function definition into Python 3.x


Converting Python 2.x function definition into Python 3.x

By : user3851487
Date : October 16 2020, 11:12 PM
around this issue I have a function definition in a Python 2.x script which takes a tuple as one of its arguments, but 2to3 has no answers nor any of my searching on how to represent the same in Python 3.x , It's simple.
code :


Share : facebook icon twitter icon
-> in python function definition

-> in python function definition


By : shiv
Date : March 29 2020, 07:55 AM
Hope that helps These are function annotations covered in PEP 3107. Specifically, the -> marks the return function annotation.
Examples:
code :
>>> def kinetic_energy(m:'in KG', v:'in M/S')->'Joules': 
...    return 1/2*m*v**2
... 
>>> kinetic_energy.__annotations__
{'return': 'Joules', 'v': 'in M/S', 'm': 'in KG'}
>>> '{:,} {}'.format(kenetic_energy(20,3000),
      kenetic_energy.__annotations__['return'])
'90,000,000.0 Joules'
def validate(func, locals):
    for var, test in func.__annotations__.items():
        value = locals[var]
        try: 
            pr=test.__name__+': '+test.__docstring__
        except AttributeError:
            pr=test.__name__   
        msg = '{}=={}; Test: {}'.format(var, value, pr)
        assert test(value), msg

def between(lo, hi):
    def _between(x):
            return lo <= x <= hi
    _between.__docstring__='must be between {} and {}'.format(lo,hi)       
    return _between

def f(x: between(3,10), y:lambda _y: isinstance(_y,int)):
    validate(f, locals())
    print(x,y)
>>> f(2,2) 
AssertionError: x==2; Test: _between: must be between 3 and 10
>>> f(3,2.1)
AssertionError: y==2.1; Test: <lambda>
Is Python 2.7 actually converting my string to UTF-8 or is the definition of isalnum() different across different machin

Is Python 2.7 actually converting my string to UTF-8 or is the definition of isalnum() different across different machin


By : Brent Shadel
Date : March 29 2020, 07:55 AM
With these it helps Strings (type str) in Python 2.7 are bytes. When you read text from a file, you get bytes, with possibly the line endings changed. Therefore, s is not an instance of type unicode.
On a str, tests like isalnum() assume that the string is ASCII text. ASCII is defined only for codes 0 to 127. Python has no idea, and can have no idea, what characters are represented by values outside this range, because the encoding is not known. é is not an ASCII character and therefore is not considered alphanumeric.
code :
u = s.decode("utf8")
Converting from c++ to python - How to declare a virtual method with no definition in python

Converting from c++ to python - How to declare a virtual method with no definition in python


By : SOFTAFZAR
Date : March 29 2020, 07:55 AM
it fixes the issue The base class in C++ is declaring a virtual method with no definition.
code :
virtual void example(paramtype, paramtype) = 0;
raise NotImplementedError()
class A:
    def example(self):
        raise NotImplementedError()

    def myMethod(self):
        self.example()

class B(A):
    # override the example method by providing the implementation
    def example(self):
        # implementation
Python can't use ** outside of function definition

Python can't use ** outside of function definition


By : user2677847
Date : March 29 2020, 07:55 AM
I wish this help you You can use it to unpack it into other dictionaries. It doesn't work in the way I assume you are trying to do it which is taken inner dictionaries from values and make them all part of the same level. From the article you referenced:
code :
>>> date_info = {'year': '2020', 'month': '01', 'day': '7'}
>>> event_info = {**date_info, 'group': "Python Meetup"}
>>> event_info
{'year': '2020', 'month': '01', 'day': '7', 'group': 'Python Meetup'}
>>> event_info = {'year': '2020', 'month': '01', 'day': '7', 'group': 'Python Meetup'}
>>> new_info = {**event_info, 'day': "14"}
>>> new_info
{'year': '2020', 'month': '01', 'day': '14', 'group': 'Python Meetup'}
Can a function in Python return an invalid Python expression, and what exactly is the definition of an valid/invalid Pyt

Can a function in Python return an invalid Python expression, and what exactly is the definition of an valid/invalid Pyt


By : Jon Atkins
Date : March 29 2020, 07:55 AM
I hope this helps . Functions do not return expressions. They return values. What the documentation says is that the value returned by bin is a string that represents a valid python expression. In fact the returned value represents a binary literal.
You can check whether a string is a valid expression by evaluating it using eval:
shadow
Privacy Policy - Terms - Contact Us © bighow.org