Property or method?

I have difficulty understanding something:

Say we have a class in csharp:

class z_class()
{
  public int[, ,] some_name { get; protected set;}
}

is some_name a property or a method?

How would it look in python?
Is it something like this?

class z_class():
    def __init__(self):
        self.some_name = []
    @property
    def some_name_method(self):
        return self.some_name
    
    @some_name_method.setter
    def some_name_method(self,value):
        self.some_name = value

I know this works but is it equivalent to the csharp?
What does it mean that the set is protected?
What is the equivalent to protected in Python?

Thanks in advance.

some_name is a property, because when you have an instance of z_class, you do not use a method-call (which has parentheses) but just the property name:

z_class z = new z_class()
int[,,] thePropertyOfZ = z.some_name; // No parentheses

the access modifier protected means that only the instance itself, or classes that inherit from z_class can use the setter. If the modifier is set to private only the the instance itself can access it.

z_class z = new z_class()
int[,,] toSet = new int[1,1,1];
z.some_name = toSet; // does not compile, setter is not publicly accesible
1 Like

Thanks @menno,

If it is a property why is there curly brackets when defining this property?
{get;set}?

1 Like

That is just the syntax on how to write a property, that is in the C# language specification. Curly braces are typically used to denote a context change, and it is the same for a function/method.

yeah this is what confuses me.
If I see the code in csharp how do I recognize/distinguish between property and method?

A method has parentheses:

class z_class
{
  int[,,] some_name {get; protected set;}
  public void SomeMethod(int inputNumber)
  {
    // do stuff 
  }
}

z_class z = new z_class();
z.SomeMethod(10); // call method
z.some_name = new int[2,2,2]; // call property setter
1 Like

Thanks, now I get it :slight_smile:

1 Like

Make sure of course you use the set accessor in a scope that can see the protected set - i.e. in z_class implementation or in a derived class from it.

Hi @nathanletwory,

Could you please show me a small example what you mean?
set accessor in a scope ?

In your initial snippet it says

int[,,] some_name { get; protected set; }. With the class as you had you can’t do what @menno had on the last line of the accepted solution. There are two ways that specific setter can be used:

class a_class : z_class {
  void SomeFunc(int[,,] arr) {
    some_name = arr; // can access since we derive from z_class
  }

  int[,,] some_prop {
    get { return some_name; }
    set { some_name = value; } // ditto, can access, we derive
  }
}

// now can instantiate a_class, and use either some_prop or SomeFunc
// to set some_name
// we can _get_ some_name from outside the protected scope, but
// not set
z_instance = new z_class();
z_instane.some_name = new int[2,2,2]; // error, protected setter
a_instance = new a_class();
a_instance.some_prop = new int[2,2,2]; // ok, because setter is public
var arr = a_instance.some_name; // ok, because getter is public
a_instance.some_name = new int[2,2,2] // error, some_name setter still protected
2 Likes

Oh, yes. He actually mentiones it won’t work in a comment inside his previous reply.

I still do not understand how can I make a protected setter with python. I seem to be able to access both the property and the setter method from the instance.

class code:

# cPython3
class z():
    
    # Initializer
    def __init__(self):
        self.prop_1 = None
        self.prop_2 = 3
    
    @property
    def Prop1(self):
        return self.prop_1
    
    @Prop1.getter
    def Prop1(self):
        return self.prop_1
    
    @Prop1.setter
    def Prop1(self, val):
        self.prop_1 = val
    
    @property
    def Prop2(self):
        return self.prop_2
    
    @Prop2.getter
    def Prop2(self):
        return self.prop_2
    
    @Prop2.setter
    def Prop2(self,value):
        self.prop_2 = value


instance code:

#cPython3

from zClass import z


z_instance = z()

print(dir(z))
print("Before setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.prop_2))

z_instance.Prop1 = 4513
z_instance.Prop2 = 213

print("After setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.prop_2))

z_instance.prop_1 = 333
z_instance.prop_2 = 444

print("After setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.prop_2))

I do not fully understand.

Python doesn’t do that kind of scoping. You can get at everything Python in anything Python. You cannot shield parts of classes from (mis)usage.

Not fiddling with ‘internals’ is solely up to the developer, and to the consumers of that code.

1 Like

I mean, one can (kinda sorta) do it using two leading underscores:

But yes, not for realz:

1 Like

I always wondered why people use two underscores in object names, I thought it’s to avoid name conflicts between classes :man_facepalming:

Just as a clarification. A property in C# is kind of a mix of a class variable (field) and a method. It syntax sugar for getVariableX() and setVariableX(). You can do the same by implementing these two functions and a field variable. Using underscores you can simulate private/internal/protected access. Bytheway, you can also access private fields in C# by using reflection, so it shouldn’t be seen as a security feature, but rather as a don’t-do-this-dave hint.

2 Likes

I’m a little confused again.

class:

class z():
    
    # Initializer
    def __init__(self):
        self.prop_1 = None
        # making this property private by putting __ (double underscore) as prefix
        self.__prop_2 = 3
    
    @property
    def Prop1(self):
        return self.prop_1
    
    @Prop1.getter
    def Prop1(self):
        return self.prop_1
    
    @Prop1.setter
    def Prop1(self, val):
        self.prop_1 = val
    
    @property
    def Prop2(self):
        return self.__prop_2
    
    @Prop2.getter
    def Prop2(self):
        return self.__prop_2
    
    @Prop2.setter
    def Prop2(self,value):
        self.__prop_2 = value


instance implementation:

from zClass import z


z_instance = z()

#print(dir(z))
print("Before setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.Prop2))

z_instance.Prop1 = 4513
z_instance.Prop2 = 213

print("After setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.Prop2))

z_instance.prop_1 = 333
# ????
z_instance.__prop_2 = 444

print("After setting properties")
print("property 'prop_1' = {}".format(z_instance.prop_1))
print("property 'prop_2' = {}".format(z_instance.Prop2))

print(z_instance.__dict__)

output:

Why are _z__prop_2 and __prop_2 valuated differently?
Are they (it seems they are) different objects?

Could you please explain this with the dummies reference: Class Car: self.color = White - > porsche = Car() -> porsche.color = red

Because it appears now the porsche is both red and white :thinking:

hmm…does _z__prop_2 actually mean the default class property?..I’ll test

Nope…I’m still confused :roll_eyes:

I don‘t know if the concept of properties are very pythonic, because I see no benefit over writing two simple set and get functions directly, at least if you need to declare properties like this.

You may get a better understanding of all this when learning C#. What it looks like to me is, that very often you collide with C# concepts, which perfectly make sense in C# but are quite irrational in Python. This is also my problem with IronPython, because even for me, as being quite experienced with dotnet C# and CPython, I just get frustrated by mixing concepts in IronPython. It simply doesn’t work very well for both paradigms.
I think you really have a more simple life when using C#, being able to debug with visual studio. This also makes all your printing irrelevant, since a good debugger just tells you whats going on in every single line of code.( Rhino Python debugging is not bad either, just not as powerful)

1 Like

I believe, and maybe I’m wrong but anyways I try, by translating C# to Python I learn to understand C# better. Thus, learn to create C# code. The problem is there are still a lot of things I don’t know about Python (and programming in general). Trying to “hit two rabbits with a single bullet” (literal translation of a Bulgarian saying). I want to learn both C#, get better at Python and grok General Programming at the same time.

https://trello-attachments.s3.amazonaws.com/58e3f7c543422d7f3ad84f33/5d01751678f28538bd992907/c2f6bf103b9e2b862c2f3d14dd7056b1/Dwn-TyOWkAI5YAm.jpg

3 Likes