Dumb question about python

Hi all,

Just trying to convert some codes written in c# to Python. In Python, how can I pass a variable with type as in c# (Point3d pt)?

C# -----------------------------------

public double GetZVal(Point3d pt)
{
return pt.Z;
}

Python ------------------------------

def GetZVal(pt):
return pt.Z

@woojsung , You cannot declare variables in Python, since python doesn’t have “declaration” nor “variables” the way it exists in C sense. At least that’s what I know.

@su.lwpac, Thanks for the reply. Then does this mean the autocomplete for the type doesn’t work for “pt” in this context? If so what is a workaround for Python coders? Thanks,

If the input is point you can get X,Y,Z values, why you need this def GetZVal(pt):?

I was working on a class and its method.

HI @woojsung,

In Python you don’t need to declare the variables by type, you simply define them in the scope that you need them. Other than that, the variables behave like in every other language.

In Python classes, there are also no private, public, or protected attributes and/or methods.
Everything is basically public, but what is often done for legibility is that supposedly private symbol names have an underscore as prefix (e.g. self._private_variable = 10). That’s purely visual though and not mandatory.
Since, there is no privacy so to speak, you also don’t really need get- or set-methods, expect maybe for code structuring. You simply can access every attribute or method by its description (e.g. MyClass.my_method()).

You seem to be on the right track.
Here’s an example on how I would probably do this:

import Rhino.Geometry as rg

class SomeClass:
    def __init__(self):
        """Default constructor initialises this class"""
        self._pt = rg.Point3d.Origin # _pt is a "private" attribute
   
    def get_zval(self):
        """Returns the z-coordinate of _pt."""
        return self._pt.z


if __name__ == "__main__":
    sc = SomeClass() # instatiate a class instance

    print( sc.get_zval() )
    # Alternatively :    
    print( sc._pt.z ) # only fake private

1 Like

That’s not (entirely) accurate, you just need two leading underscores:

2 Likes

Correct. It will generally only work on input parameter objects. And when you implement something from a namespace you’ve imported.

I keep the RhinoCommon API open, and use help() and dir(). And have memorised quite a lot :man_shrugging:

2 Likes

Awesome, thank you guys for the answers!!

1 Like

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)